我使用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);
}
}
我的代碼是工作的罰款直接create()
的訪問本Role
類時,但試圖利用我的新UserRole
類來執行相同的任務,我得到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()
工作正常,但UserRole::create()
沒有。
我試過了。雖然它對主表起作用,但所有關係表查詢都搞砸了,即使在繼承模型中聲明瞭它們的表名。 – kjdion84
@ kjdion84是否發佈了包配置文件,並將'model.role'鍵更改爲指向您的自定義模型? – patricus