I created a many to many relationship between User and Language:
Models/User
public function languages()
{
return $this->belongsToMany(Language::class);
}
Models/Language
public function users()
{
return $this->belongsToMany(User::class);
}
Migrations/Languages
Schema::create('languages', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->text('sigla');
$table->timestamps();
});
Migrations/Users
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->text('img')->nullable();
$table->rememberToken();
$table->string('api_token', 64)->unique()->default(Str::random(50));
$table->timestamps();
});
Migrations/User_Language
Schema::create('user_language', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('lang_id');
$table->unsignedInteger('user_id');
$table->timestamps();
$table->unique('lang_id','user_id');
$table->foreign('lang_id')->references('id')->on('languages')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
unfortunately the latter gives me the following error:
SQLSTATE[HY000]: General error: 3780 Referencing column 'lang_id' and referenced column 'id' in foreign key constraint 'user_language_lang_id_foreign' are incompatible. (SQL: alter table `user_language` add constraint `user_language_lang_id_foreign` foreign key (`lang_id`) references `languages` (`id`) on delete cascade)