扩展模型类会导致找不到列错误

时间:2017-04-25 15:55:00

标签: php laravel

我正在使用https://github.com/spatie/laravel-permission

我创建了一个扩展Role类的新类。以下是Role的代码:

<?php

namespace Spatie\Permission\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Traits\HasPermissions;
use Spatie\Permission\Exceptions\RoleDoesNotExist;
use Spatie\Permission\Contracts\Role as RoleContract;
use Spatie\Permission\Traits\RefreshesPermissionCache;

class Role extends Model implements RoleContract
{
    use HasPermissions;
    use RefreshesPermissionCache;

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    public $guarded = ['id'];

    /**
     * Create a new Eloquent model instance.
     *
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setTable(config('laravel-permission.table_names.roles'));
    }

    /**
     * A role may be given various permissions.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function permissions()
    {
        return $this->belongsToMany(
            config('laravel-permission.models.permission'),
            config('laravel-permission.table_names.role_has_permissions')
        );
    }

    /**
     * A role may be assigned to various users.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function users()
    {
        return $this->belongsToMany(
            config('auth.model') ?: config('auth.providers.users.model'),
            config('laravel-permission.table_names.user_has_roles')
        );
    }

    /**
     * Find a role by its name.
     *
     * @param string $name
     *
     * @throws RoleDoesNotExist
     *
     * @return Role
     */
    public static function findByName($name)
    {
        $role = static::where('name', $name)->first();

        if (! $role) {
            throw new RoleDoesNotExist();
        }

        return $role;
    }

    /**
     * Determine if the user may perform the given permission.
     *
     * @param string|Permission $permission
     *
     * @return bool
     */
    public function hasPermissionTo($permission)
    {
        if (is_string($permission)) {
            $permission = app(Permission::class)->findByName($permission);
        }

        return $this->permissions->contains('id', $permission->id);
    }
}

直接为Role访问此create()类时,我的代码工作正常,但尝试使用我的新UserRole类执行相同的任务,我得到{{1尝试创建新的Column not found时出现数据库错误。

以下是Role类:

UserRole

因此namespace App; use Spatie\Activitylog\Traits\LogsActivity; use Spatie\Permission\Models\Role; class UserRole extends Role { use LogsActivity; /** * The attributes that should be logged. * * @var array */ protected static $logAttributes = ['name', 'permissions']; } 工作正常,但Role::create()没有。

2 个答案:

答案 0 :(得分:0)

将名称更改为Role,然后将我的使用条款更改为as SpatieRole已解决此问题。我猜这是Eloquent的某种类名关系问题。

答案 1 :(得分:0)

如果未在Eloquent模型上定义$table属性,则表名称是从模型名称派生的。因此,Role模型默认使用roles表。 UserRole模型默认会查找user_roles表。

由于您仍希望使用同一个表,但更改了模型名称,因此需要在新模型上定义$table属性,以使其查看roles表。 / p>

class UserRole extends Role
{
    protected $table = 'roles';

    // ...
}