2

Hey guys so I'm using spatie binary uuid package and I had few doubts Things done so far: User.php Migration:

 public function up()
  {
    Schema::create('users', function (Blueprint $table) {
        $table->uuid('uuid');
        $table->primary('uuid');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
 }

Role migration just have basic field called "name" with timestamps

Pivot table: role_user

public function up()
{
    Schema::create('role_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('role_id')->unsigned()->nullable()->index();
        $table->uuid('user_uuid');

    });
}

I know this is terribly wrong and I don't know what to do, I'm trying to save the Role model via this call

$uuid = '478d7068-ae64-11e8-a665-2c600cf6267b';
$model = User::withUuid($uuid)->first();
$model->roles()->save(new Role(['name' => 'Admin']));

it doesn't work, where am I going wrong? I think it has something to do with role_user migration

User.php

public function roles()
{
    return $this->belongsToMany(Role::class);
}
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
Rohan Shukla
  • 89
  • 1
  • 12

2 Answers2

1

try this, pivot migration:

public function up()
{
    Schema::create('role_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('role_id')->unsigned()->nullable()->index();
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        $table->uuid('user_uuid');
       $table->foreign('user_uuid')->references('uuid')->on('users')->onDelete('cascade');
    });
}

roles relation:

public function roles(){
  return $this->belongsToMany(Role::class,'role_user','user_uuid','role_id');
}

please let me know if it didn't work

Hussein
  • 1,143
  • 1
  • 9
  • 16
  • Your migration seems to be not working, I added foreign value given by you and it gives me Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced ina foreign key constraint – Rohan Shukla Sep 02 '18 at 06:35
  • Nevermind it works but somehow that error comes up when I seed the UsersTableSeeder – Rohan Shukla Sep 02 '18 at 06:38
  • this is not a migration error, maybe you are trying to delete the relation/user/role somewhere in the code – Hussein Sep 02 '18 at 06:38
  • ok disable foreign key check in the seed file, check this answer: https://stackoverflow.com/questions/34298639/laravel-migrations-nice-way-of-disabling-foreign-key-checks – Hussein Sep 02 '18 at 06:40
  • in the seed file add this: `\Illuminate\Support\Facades\DB::statement('SET FOREIGN_KEY_CHECKS=0;');` and once done deleting enable it: `\Illuminate\Support\Facades\DB::statement('SET FOREIGN_KEY_CHECKS=1;');` – Hussein Sep 02 '18 at 06:44
  • Yes I'll definitely do this.. Thanks man I was first checking that link and in it the method deleteForiegnKeyConstrain wasn't working I'll try this though – Rohan Shukla Sep 02 '18 at 09:29
0

you should edit your relation to this

return $this->belongsToMany(Role::class,'role_user','user_uuid','role_id');

if you say about your error we better can help you

atf.sgf
  • 458
  • 2
  • 4
  • 16