Pivot Model
The package trait could also be used on pivot models but you should adjust some things to make everything work.
Because the trait introduces a new relation your base model needs a primary key - we will use an auto-increment id column. If you want to use an UUID string column or another key you have to set/adjust more things (tell the model and trait which is your primary key, adjust migration ...) but even this is possible.
RoleUser.php
1
use Illuminate\Database\Eloquent\Relations\Pivot;
2
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
3
use Astrotomic\Translatable\Translatable;
4
5
class RoleUser extends Pivot implements TranslatableContract
6
{
7
use Translatable;
8
9
public $incrementing = true;
10
}
Copied!
create_role_user_table.php
1
Schema::create('role_user', function(Blueprint $table) {
2
$table->increments('id');
3
$table->integer('user_id')->unsigned();
4
$table->integer('role_id')->unsigned();
5
6
$table->unique(['user_id', 'role_id']);
7
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
8
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
9
});
Copied!
Last modified 2yr ago
Copy link