Мне нужна ваша помощь по проблеме с внешним ключом.
У меня есть три таблицы users - role_user - роль с внешним ключом для role_user и таблица ограничений внешнего ключа для role_user Но ничего не работает. Я вставляю в таблицу 'users', но ничего в таблице 'role_user' и таблице 'role'
- 'role_user', берут 'user_id' и 'role_id'
- 'role' стол взять «роль»
Большое спасибо за вашу помощь
см. мой код:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
//$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('role');
//$table->foreign('role')->references('id')->on('roles');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
//$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->string('role');
//$table->foreign('role')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
//$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->bigInteger('role_id')->unsigned();
$table->bigInteger('user_id')->unsigned();
//$table->foreign('role_id')->references('id')->on('roles');
//$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateForeignKeysForRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('role_user', function (Blueprint $table) {
//$table->engine = 'InnoDB';
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('role_user', function(Blueprint $table){
$table->dropForeign('role_user_user_id_foreign');
$table->dropForeign('role_user_role_id_foreign');
});
}
}
Модель пользователя
public function roles(){
return $this->belongsToMany('App\Role');
}
/*
public function role(){
return $this->belongsToMany(\App\RoleUser::class);
}
*/
public function hasAnyRoles($roles){
if($this->role()->whereIn('role', $roles)->first()){
return true;
}
return false;
}
public function hasRole($role){
if($this->roles()->where('role', $role)->first()){
return true;
}
return false;
}
Ролевая модель:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $guarded=[];
public function users(){
return $this->belongsToMany('App\User:');
}
/*
public function admins(){
return $this->belongsToMany(\App\RoleUser::class);
}
*/
}