简体   繁体   中英

Many-to-many relation Laravel

what Am I doing wrong? Very very simply example:

Permission model:

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();
    }
}

Role model:

<?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();
    }
}

Role migration:

<?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');
    }
}

Permission migration:

<?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');
    }
}

role_permission migration:

<?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 Tinker:

>>> $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))'

It's so simply code and I'm doing everything what I see in course. Couse is based on Laravel 5, but I have Laravel 6, maybe this is the problem? Please help!

$r is still the instance of new App\\Role and not $r->first()

You have missed the assigning $r->first() to a variable.

Try

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM