Giter Club home page Giter Club logo

Comments (5)

drbyte avatar drbyte commented on May 26, 2024
  1. How are you registering roles to the model? It would be helpful to see what your code is for that, since it should have already finished persisting things and letting go of what was specified to be attached. Seeing your code would help.
  2. In your listener, perhaps you can call $user->fresh() to have a clean unique instance before updating its properties and saving?

from laravel-permission.

Moundlen avatar Moundlen commented on May 26, 2024

Hi, here is a simple code to reproduce created a new empty laravel 11 project.

<?php

use Illuminate\Support\Facades\Route;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

Route::get('/', function () {
    $user = new \App\Models\User([
        'name' => \Illuminate\Support\Str::random(),
        'password' => \Illuminate\Support\Str::random(),
        'email' => \Illuminate\Support\Str::random() . '@example.com',
    ]);
    if (Role::where('name', 'writer')->doesntExist()) {
        $role = Role::create(['name' => 'writer']);
        $permission = Permission::create(['name' => 'edit articles']);
        $role->givePermissionTo($permission);
    }
    $user->assignRole('writer');

    $user->save();
    $user = $user->fresh();
    $user->save();
});

from laravel-permission.

drbyte avatar drbyte commented on May 26, 2024

Thanks for the code sample.

Yes, if you're creating a User with new, then it is not yet persisted into the database, and attached pivots are getting duplicated when calling save().

However, if you first ->save() that unpersisted User before calling assignRole()/etc, subsequent saves will not be problematic.

Also, if you use User::create() instead of new User, none of these problems occur.

from laravel-permission.

drbyte avatar drbyte commented on May 26, 2024

@erikn69 I'm interested in your thoughts about how to avoid these "Duplicate entry" errors when attaching to an un-persisted model.

Ref: #2419 #2420 #2574
Ref: https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php

from laravel-permission.

erikn69 avatar erikn69 commented on May 26, 2024

I think it is a problem that has always been present.
A validation could be added,
I would have to do tests, I think that adding wasRecentlyCreated would solve it

$class::saved(
function ($object) use ($permissions, $model, $teamPivot) {
if ($model->getKey() != $object->getKey()) {
return;
}
$model->permissions()->attach($permissions, $teamPivot);
$model->unsetRelation('permissions');
}
);
}

from laravel-permission.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.