Single Table
With this strategy, the translatable model table does not physically contain fields for translatable attributes.
They are stored separately in the single global translations
table, but the model can work with them as it would with regular model attributes.
See an example of the database structure:
The translations
table uses a polymorphic relationship and also stores the name of the attribute being translated.
Let's make, for example, a translatable Book
model that has 2 translatable attributes: title
and description
.
Add the Nevadskiy\Translatable\Strategies\SingleTable\HasTranslations
trait to your model and specify translatable
attributes.
The model class may look like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Nevadskiy\Translatable\Strategies\SingleTable\HasTranslations;
class Book extends Model
{
use HasTranslations;
protected $translatable = [
'title',
'description',
];
}
The simplest books
table migration might look like this:
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
Note that there are no translatable fields in the table.
They will be placed in the single translations
table, so you need to add it to the project:
php artisan vendor:publish --tag=translations-migration
Execute the migrate
command:
php artisan migrate
You can specify a custom Translation
model globally in the App\Providers\AppServiceProvider
class:
use Nevadskiy\Translatable\Strategies\SingleTable\SingleTableStrategy;
use App\Translation;
public function boot()
{
SingleTableStrategy::useModel(Translation::class);
}