我正在使用databaseeder将控制器的名称存储在权限表中,但我没有弄错我为什么会出错。该表与角色表的关系使用belongsToMany关系。 这是我的Role.php
public function permissions()
{
return $this->belongsToMany(Permission::class)->withTimeStamps();
}
这是我的Permission.php
protected $fillable=['id','name','controller'];
public function roles()
{
return $this->belongsToMany(Role::class)->withTimeStamps();
}
这是我的 databaseSeeder.php
$superrole= Role::create([
'name'=>'super admin',
]);
$permission_ids = [];
$route_name = [];
$controllers = [];
foreach (Route::getRoutes()->getRoutes() as $route)
{
$action = $route->getAction();
if (array_key_exists('as', $action)) {
$route_name = $action['as'];
}
if (array_key_exists('uses', $action)) {
$controllers = $action['uses'];
}
$permission_check = Permission::whereIn('name' , $route_name)
->whereIn('controller' , $controllers)->first();
if(!$permission_check)
{
$permission = new Permission;
$permission->controller = $controllers;
$permission->name = $route_name;
$permission->save();
$permission_ids[] = $permission->id;
}
}
$superrole->permissions()->attach($permission_ids);
但是当我尝试运行db:seed iam时,出现此错误] 1 这是dd($ controllers)