创建新用户

时间:2017-07-20 21:57:56

标签: laravel-5 pivot-table

一直在网上搜索我的问题,但找不到答案: - (

我通过拍摄视频,在我的Laravel项目中创建了角色/权限。它使用数据透视表。 我的数据库包含php artisan make之后的以下标准Laravel表:auth(和migrate):

  • 用户
  • password_resets

对于角色/权限功能,我创建了以下表格:

  • roles(id,name)
  • 权限(id,name)
  • roles_permissions(role_id,permission_id)
  • users_roles(user_id,role_id)
  • users_permissions(user_id,permission_id)

我还创建了一个特性:

<?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);;
}

}

我的UsersController看起来像这样(创建方法):

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.');;

}

我想要做的是为新创建的用户提供默认的用户角色(id =&#39; 10&#39;,name =&#39; user&#39;在users_roles表中)。< / p>

当我使用方法roles()为新创建的用户创建新记录时,我收到一条错误消息,指出列&#39;角色&#39;在角色表中不存在。这是真的,因为它只有一个id&amp;名称栏。

有人可以告诉我为什么吗?甚至更好地给我一些代码示例来解决这个问题?

提前致谢。

0 个答案:

没有答案