Spatie role plugin for Laravel + seed users with roles, no roles assigned
P粉637866931
P粉637866931 2023-09-05 23:24:56
0
1
407
<p>In Laravel 9 I tried adding a role to the user</p> <p>Spatie just processed everything without errors, but in the database the relationship between role and user does not appear in the model for table "model_has_role"</p> <p><code>I created a similar character</code></p> <pre class="brush:php;toolbar:false;">$roleName = 'MyRole'; $guardName = 'myGuard'; $roleObj = Role::create(['guard_name' => $guardName, 'name' => $roleName]);</pre> <p><code>Then create a user, for example </code></p> <pre class="brush:php;toolbar:false;">$user = new User($userDatas);</pre> <p><code>I collected all the data from the model that should be filled in</code></p> <pre class="brush:php;toolbar:false;">$arrayWithUsersData = []; foreach($users as $user) { if($users !== NULL) { $arrayWithUsersData[] = $user->getAttributes(); } }</pre> <p><code>After this, I tried to insert all users into the database at once, so I used </code></p> <pre class="brush:php;toolbar:false;">DB::table('users')->insert($arrayWithUsersData);</pre> <p>Finally I tried any possible way to assign roles in foreach $users Like: </p> <pre class="brush:php;toolbar:false;">$user->assignRole($roleName); $user->assignRole($roleName, $guardName); $user->assignRole([$roleName, $guardName]); $user->assignRole($myRole); $user->assignRole($myRole, $guardName); $user->assignRole([$myRole, $guardName]); $user->syncRoles($myRole); $user->syncRoles($myRole, $guardName); $user->syncRoles([$myRole, $guardName]);</pre> <p>But no error is given but still no relationship is created</p>
P粉637866931
P粉637866931

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!