Comments (5)
- 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.
- In your listener, perhaps you can call
$user->fresh()
to have a clean unique instance before updating its properties and saving?
from laravel-permission.
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.
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.
@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.
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
laravel-permission/src/Traits/HasPermissions.php
Lines 405 to 414 in ce67a8b
from laravel-permission.
Related Issues (20)
- team_id doesn't have a default value HOT 5
- assignRole and syncRoles fail for model with ulid type id HOT 2
- Documentation for v6/main unaccessible at spatie.be HOT 4
- Wrong octane event listener HOT 2
- Too much Role-Models loaded HOT 3
- Enums shouldn't be casted in the model HOT 8
- assignRole > getModel() > null
- Timestamps in migration for sql server HOT 4
- Target class [Spatie\Permission\Middlewares\PermissionMiddleware] does not exist. HOT 2
- DOCS - Side menu dissapeared HOT 4
- Problem upgrading with existing data permissions/roles HOT 1
- withoutPermission scope from HasPermissions returns users from outside the team HOT 2
- Laravel 11 compatibility issues HOT 4
- Problem with middleware definition method in the controller in Laravel 11
- Problem with denied access using the middleware aliases role, permission, and role_or_permission in Spatie Permission version 6 and higher. HOT 1
- Problem when trying to migrate laravel-permission migrations HOT 2
- Docs are not uptodate with current implementation HOT 9
- Diffs are not properly displayed in generated docs HOT 4
- Bug @can OR $user->can OR $user->hasAnyPermission($permission) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-permission.