I'm new to laravel. Help please.
Error in the console when trying to migrate:
Migrating: 2022_03_15_224441_add_permission_menu_table
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `permission_menu` add constraint `permission_menu_menu_id_foreign` foreign key (`menu_id`) re
ferences `menus` (`id`) on delete cascade)
at D:\DEV\OpenServer\domains\crm.loc\vendor\laravel\framework\src\Illuminate\Database\Connection.php:712
708▕ // If an exception occurs when attempting to run a query, we'll format the error
709▕ // message to include the bindings with SQL, which will make this exception a
710▕ // lot more helpful to the developer instead of just the database's errors.
711▕ catch (Exception $e) {
➜ 712▕ throw new QueryException(
713▕ $query, $this->prepareBindings($bindings), $e
714▕ );
715▕ }
716▕ }
1 D:\DEV\OpenServer\domains\crm.loc\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
2 D:\DEV\OpenServer\domains\crm.loc\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOStatement::execute()
Migration file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddPermissionMenuTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('permission_menu', function (Blueprint $table) {
//
$table->integer('permission_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->bigInteger('menu_id')->unsigned();
$table->foreign('menu_id')->references('id')->on('menus')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// Schema::table('permission_menu', function (Blueprint $table) {
// $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
// $table->foreign('menu_id')->references('id')->on('menus')->onDelete('cascade');
// });
}
}
// the second migration file
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMenusTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('menus', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('path');
$table->integer('parent');
$table->string('type');
$table->integer('sort_order')->default(100);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('menus');
}
}
Model File:
<?php
namespace App\Modules\Admin\Menu\Models;
use App\Modules\Admin\Role\Models\Permission;
use App\Modules\Admin\Users\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Menu extends Model
{Й
use HasFactory;
const MENU_TYPE_FRONT = 'front';
const MENU_TYPE_ADMIN = 'admin';
public function perms() {
return $this->belongsToMany(Permission::class, 'permission_menu');
}
public function scopeFrontMenu($query, $user) {
return $query->
where('type', self::MENU_TYPE_FRONT)->
whereHas('perms', function($q) use($user) {
$arr = collect($user->getMergedPermissions())->
map(function($item) {
return $item['id'];
}
);
$q->whereIn('id', $arr->toArray());
});
}
public function scopeAdminMenu($query, $user) {
return $query->where('type', self::MENU_TYPE_ADMIN);
}
public function scopeMenuByType($query, $type) {
return $query->where('type', $type)->orderBy('parent')->orderBy('sort_order');
}
}
I do not know what other details to describe to make it clearer what the problem is. Please ask me if need what else. I do not know what other details to describe to make it clearer what the problem is. Please ask me if need what else. I do not know what other details to describe to make it clearer what the problem is. Please ask me if need what else.
CodePudding user response:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddPermissionMenuTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('permission_menu', function (Blueprint $table) {
//
$table->bigInteger('permission_id')->unsigned();
Change integer to bigInteger on permission id
CodePudding user response:
I decided so, for the permission_menus table I changed:
Schema::create('permission_menu', function (Blueprint $table) {
//
$table->integer('permission_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->bigInteger('menu_id')->unsigned();
$table->foreign('menu_id')->references('id')->on('menus')->onDelete('cascade');
});
And for the menus table:
Schema::create('menus', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('path');
$table->integer('parent');
$table->string('type');
$table->integer('sort_order')->default(100);
$table->timestamps();
});
And everything worked. Thanks!
I also tried to do this for the menus table left:
$table->increments('id');
And for the permission_menu table:
$table->integer('menu_id')->unsigned();
and everything worked too. It turns out to be a mismatch of two fields of two connected tables.