如何在Laravel中解决数据库Seeder问题

时间:2019-10-02 12:37:53

标签: php laravel eloquent

我正在使用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时,出现此错误![enter image description here] 1 这是dd($ controllers) enter image description here

和dd($ route_name) enter image description here

0 个答案:

没有答案