Builder.php第2440行中的BadMethodCallException:调用未定义的方法Illuminate \ Database \ Query \ Builder :: getIdArray()

时间:2016-10-12 12:08:48

标签: laravel-5

我在laravel中创建角色方法,但是当我启动页面时出现此错误。如果有人有任何想法你可以分享它。谢谢。

这是我的用户模型

 <?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    
   public function roles(){
       return $this->belongsTo(App\Role);
   }
   
   public function isEmployee(){
       return ($this->roles()->count()) ? true : false; 
   }
   
   public function hasRole($role){
       return (in_array($this->roles->pluck("names"), $role));
   }
    
   private function getIdInArray($array, $term){
       
       foreach ($array as $key => $value){
           if($value == $term){                   
               return $key;
           }
       }
       
       throw new UnexpectedValueException;           
   }
   
   public function makeEmployee($title){
       $assigned_roles = array();
       
       $roles = Role::all()->pluck("name", "id");
       
       switch($title){
           case 'super_admin':
               $assigned_roles[] = $this->getIdArray($roles, 'create');
               $assigned_roles[] = $this->getIdArray($roles, 'update');
           case 'admin':
               $assigned_roles[] = $this->getIdArray($roles, 'delete');
               $assigned_roles[] = $this->getIdArray($roles, 'ban');
           case 'moderator':
               $assigned_roles[] = $this->getIdArray($roles, 'kickass');
               $assigned_roles[] = $this->getIdArray($roles, 'lemons');
               break;
           default:
               throw new \Exception("The employee status entered does not exist");
       }
       $this->roles()->sync($assigned_roles);
   }       
}

0 个答案:

没有答案