PHPackages                             goodvin/langust - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. goodvin/langust

ActiveLibrary[Framework](/categories/framework)

goodvin/langust
===============

A Laravel package

v0.1-alpha(11y ago)031MITPHPPHP &gt;=5.4.0

Since Mar 23Pushed 11y ago1 watchersCompare

[ Source](https://github.com/esomkin/Langust)[ Packagist](https://packagist.org/packages/goodvin/langust)[ RSS](/packages/goodvin-langust/feed)WikiDiscussions master Synced today

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Langust
=======

[](#langust)

This is a Laravel package for translatable models.

\##Demo

Creating new translations

```
App\Models\Article::create([

	'url' => 'your-url',
	'en' => [

		'name' 	=> 'Article english name',
		'title' => 'Article english title',
	],
	'fr' => [

		'name' 	=> 'Article french name',
		'title' => 'Article french title',
	],
]);
```

Getting translated attributes

```
$article = App\Models\Article::where('url', '=', 'your-url')->first();

App::setLocale('en');
echo $article->name;

echo $article->translate('en')->name;
echo $article->en->name;
```

Setting translated attributes

```
$article->url 			= 'your-url-change';

$article->name			= 'Article english name change';
$article->en->name		= 'Article english name change';
$article->translate('fr')->title= 'Article french title change';

$article->save();
```

or

```
$article->save([

	'url'	=> 'your-url-change',
	'es' 	=> [

		'name' 	=> 'Article spain name',
		'title' => 'Article spain title',
	],
]);
```

or

```
$article->fill([

	'en' => [

		'name' 	=> 'Article english name',
		'title' => 'Article english title',
	],
	'es' => [

		'name' 	=> 'Article spain name',
		'title' => 'Article spain title',
	],

])->save();
```

\##Installation in 4 steps

\###Step 1: Install package

Add the package in your composer.json by executing the command.

```
composer require goodvin/langust:dev-master

```

Next, add the service provider to `config/app.php`

```
'Goodvin\Langust\LangustServiceProvider',

```

\###Step 2: Migrations

For example, we need to translate `Article` model. It is require an extra `ArticleLang` model.

```
Schema::create('articles', function(Blueprint $table){

    $table->increments('id');
    $table->string('url', 200);
    $table->timestamps();
});
```

```
Schema::create('article_langs', function(Blueprint $table){

    $table->increments('id');
    $table->string('name', 200);
    $table->string('title', 200);
    $table->integer('article_id')->unsigned();
    $table->enum('lang', [

        'en',
        'fr',
        'es',
    ])->index();

    $table->unique([

        'article_id',
        'lang'
    ]);

    $table->foreign('article_id')
        ->references('id')
        ->on('articles')
        ->onDelete('cascade');
});
```

\###Step 3: Models

1. The translatable model `Article` should use the trait `Goodvin\Langust\Langust`.
2. The convention for the translation model is `ArticleLang`.

```
// /app/Models/Article.php
