gpt4 book ai didi

php - 多对多关系 Laravel

转载 作者:行者123 更新时间:2023-11-29 09:25:59 25 4
gpt4 key购买 nike

我做错了什么?非常非常简单的例子:

权限模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
protected $fillable=['name'];
public function roles()
{
return $this->belongsToMany('App\Role', 'role_permission')->withTimestamps();
}
}

榜样:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
protected $fillable=['name'];
public function permissions()
{
return $this->belongsToMany('App\Permission', 'role_permission')->withTimestamps();
}
}

角色迁移:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRoleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$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 CreatePermissionTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('permissions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('permissions');
}
}

角色权限迁移:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRolePermissionTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('role_permission', function (Blueprint $table) {
$table->unsignedBigInteger('role_id');
$table->foreign('role_id')->references('id')->on('roles');
$table->unsignedBigInteger('permission_id');
$table->foreign('permission_id')->references('id')->on('permissions');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('role_permission');
}
}

artisan 修补匠:

>>> $p=new App\Permission
=> App\Permission {#3009}
>>> $r=new App\Role
=> App\Role {#3003}
>>> $p->first()
=> App\Permission {#3017
id: 1,
name: "permisja",
created_at: "2020-01-06 01:20:34",
updated_at: "2020-01-06 01:20:34",
}
>>> $r->first()
=> App\Role {#3021
id: 1,
name: "rola",
created_at: "2020-01-06 01:20:39",
updated_at: "2020-01-06 01:20:39",
}
>>> $r->permissions()->attach(1)
Illuminate/Database/QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'role_id' cannot be null (SQL: insert into `role_permission` (`created_at`, `permission_id`, `role_id`, `updated_at`) values (2020-01-06 01:24:16, 1, ?, 2020-01-06 01:24:16))'

代码非常简单,我正在做我在类(class)中看到的一切。 Couse基于Laravel 5,但我有Laravel 6,也许这就是问题所在?请帮忙!

最佳答案

$r 仍然是 new App\Role 的实例,而不是 $r->first()

您错过了将 $r->first() 分配给变量。

尝试

$rl = $r->first();
$rl->permissions()->attach(1)

$r->first()->permissions()->attach(1)

关于php - 多对多关系 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59605732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com