Been searching the internet to get an answer to my problem but can't find it :-(
I created a Roles/Permissions ability in my Laravel project by taking a videocourse. It uses pivot tables. My database contains the following standard Laravel tables after php artisan make:auth (and migrate):
- users
- password_resets
For the roles/permissions ability I created the following tables:
- roles (id, name)
- permissions (id, name)
- roles_permissions (role_id, permission_id)
- users_roles (user_id, role_id)
- users_permissions (user_id, permission_id)
I also created a Trait:
<?php
namespace App\Permissions;
use App\{Role, Permission};
trait HasPermissionsTrait
{
public function givePermissionTo(...$permissions)
{
$permissions = $this->getAllPermissions(array_flatten($permissions));
if ($permissions === null) {
return $this;
}
$this->permissions()->saveMany($permissions);
return $this;
}
public function withdrawPermissionTo(...$permissions)
{
$permissions = $this->getAllPermissions(array_flatten($permissions));
$this->permissions()->detach($permissions);
return $this;
}
public function updatePermissions(...$permissions)
{
$this->permissions()->detach();
return $this->givePermissionTo($permissions);;
}
public function hasRole(...$roles)
{
foreach ($roles as $role) {
if ($this->roles->contains('name', $role)) {
return true;
}
}
return false;
}
public function hasPermissionTo($permission)
{
return $this->hasPermissionThroughRole($permission) || $this->hasPermission($permission);
}
protected function hasPermissionThroughRole($permission)
{
foreach ($permission->roles as $role) {
if ($this->roles->contains($role)) {
return true;
}
}
return false;
}
protected function hasPermission($permission)
{
return (bool) $this->permissions->where('name', $permission->name)->count();
}
protected function getAllPermissions(array $permissions)
{
return Permission::whereIn('name', $permissions)->get();
}
public function roles()
{
return $this->belongsToMany(Role::class, 'users_roles');
}
public function permissions()
{
return $this->belongsToMany(Permission::class, 'users_permissions');
}
public function giveRoleTo(...$roles)
{
$roles = $this->getAllRoles(array_flatten($roles));
if ($roles === null) {
return $this;
}
$this->roles()->saveMany($roles);
return $this;
}
public function withdrawRoleTo(...$roles)
{
$roles = $this->getAllRoles(array_flatten($roles));
$this->roles()->detach($roles);
return $this;
}
public function updateRoles(...$roles)
{
$this->roles()->detach();
return $this->giveRoleTo($roles);;
}
}
My UsersController look like this (the create method):
public function store(CreateUserRequest $request)
{
$user = User::create([
'achternaam' => request('achternaam'),
'voorletters' => request('voorletters'),
'district_id' => request('district_id'),
'gemeente_id' => request('gemeente_id'),
'minister_id' => request('minister_id'),
'periode_van' => request('periode_van'),
'periode_tot' => request('periode_tot'),
'email' => request('email'),
'password' => bcrypt(request('password')),
'active' => false
]);
return redirect('/home')->withInfo('Een account activatie mail is verstuurd.');;
}
What I would like to do is to give a newly created user a default role of user (id='10', name='user' in the users_roles table).
When I use the method roles() to create a new record for that newly created user I get an error saying that the column 'role' does not exist in the roles table. Which is true because it only has an id & name column.
Can someone tell me why? And even better give me some code example to fix this problem?
Thanks in advance.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2uNWOj1
via IFTTT
Aucun commentaire:
Enregistrer un commentaire