I work on a project, and this is the way I solved your problem.
Let's say we work with Posts. So, I've created 2 Models Post and PostTranslation, 2 migrations create_posts_table and create_posts_translation_table, single controller PostsController, and a Model, a Controller and a migration for Language
create_posts_table.php
Schema::create('posts', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->timestamps();
});
create_posts_translation_table.php
Schema::create('posts', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->unsignedInteger('lang_id');
$table->unsignedInteger('post_id');
$table->string('title');
$table->text('body');
$table->string('slug');
$table->timestamps();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('lang_id')->references('id')->on('lang')->onDelete('cascade');
});
Post.php
<?php
class Post extends Model
{
public function translation()
{
$lang = Language::where('language', session('applocale'))->first()->id ?? Language::first()->id;
return $this->hasMany(PostTranslation::class)->where('lang_id', $lang);
}
}
So, when you write
Post::with('translation')->get();
you get all posts where lang_id is session('applocale') or Language::first()->id
The key here is slug form posts_translation_table :)
And in your create_posts_migration you could add Image or tag_id or category_id
Not sure if it's the best way to do it..
If someone knows other way to do this, please help))