-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot save the user model twice in the same request #2638
Comments
|
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();
}); |
Thanks for the code sample. Yes, if you're creating a User with However, if you first Also, if you use |
@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 |
I think it is a problem that has always been present. laravel-permission/src/Traits/HasPermissions.php Lines 405 to 414 in ce67a8b
|
Currently running into the same issue. Any idea when #2658 will be merged? |
@ue-shuston hi, did you test #2658? does it work? |
@parallels999 Yes, for our use case the changes made in #2658 worked. Alternatively, changing
laravel-permission/src/Traits/HasRoles.php Line 168 in c99dca7
also resolved the problem. |
That would return an old problem: #2419 (comment) |
@parallels999 Understood, we had assumed that using |
Describe the bug
Hi,
Sorry if this has already been asked but I'm using Laravel fortify to register new users, once user is registered I automatically log him in but I have a listener that listens for Login and automatically edits the user IP and last login and saves the user again. When the user is saved again I get an error Duplicate entry '7-153-User' for key 'model_has_roles.PRIMARY'.
I fixed the issue by saving quietly but is this expected behavior ?
Now everytime I save a user in the same lifetime cycle it just crashes.
It seems to come from the use of attach instead of sync in the HasRoles trait line 168. see #2420
Did not have this issue before because I migrated from v4 to v6.
Versions
PHP version: 8.2.16
Database version: mysql 8.0.32
To Reproduce
My listener:
Expected behavior
I would expect the save to not throw an error if I save the User twice.
Environment (please complete the following information, because it helps us investigate better):
The text was updated successfully, but these errors were encountered: