Php Laravel-调用存储库中未定义的方法

Php Laravel-调用存储库中未定义的方法,php,laravel,laravel-5,laravel-5.2,Php,Laravel,Laravel 5,Laravel 5.2,为了保持代码的干净性和可扩展性,我创建了一个EloquentUserRepository 这是我的雄辩记述: 但当我在播种机上使用它时: 我得到: BadMethodCallException]调用未定义的方法 Illumb\Database\Query\Builder::assignRole() 这是create方法: /** * Create new resource * * @param array $data * @return mixed

为了保持代码的干净性和可扩展性,我创建了一个
EloquentUserRepository

这是我的雄辩记述:

但当我在播种机上使用它时:

我得到:

BadMethodCallException]调用未定义的方法 Illumb\Database\Query\Builder::assignRole()

这是
create
方法:

/**
     * Create new resource
     *
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return $this->model->create($data);
    }
如果我
dd
我得到
用户
模型:

App\Models\User {#605
  #fillable: array:4 [
    0 => "first_name"
    1 => "last_name"
    2 => "email"
    3 => "password"
  ]
  #hidden: array:2 [
    0 => "password"
    1 => "remember_token"
  ]
  #dates: array:1 [
    0 => "deleted_at"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:7 [
    "first_name" => "Christian"
    "last_name" => "Giupponi"
    "email" => "christian.giupponi@example.com"
    "password" => "$2y$10$sQfyoGVJxzsrs71kMW9Ul.PZ/EWbQSChhurIevwEPwwxdPYKETFOO"
    "updated_at" => "2016-05-19 07:12:18"
    "created_at" => "2016-05-19 07:12:18"
    "id" => 1
  ]
  #original: array:7 [
    "first_name" => "Christian"
    "last_name" => "Giupponi"
    "email" => "christian.giupponi@example.com"
    "password" => "$2y$10$sQfyoGVJxzsrs71kMW9Ul.PZ/EWbQSChhurIevwEPwwxdPYKETFOO"
    "updated_at" => "2016-05-19 07:12:18"
    "created_at" => "2016-05-19 07:12:18"
    "id" => 1
  ]
  #relations: []
  #visible: []
  #appends: []
  #guarded: array:1 [
    0 => "*"
  ]
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: true
  #forceDeleting: false
}
编辑:
迭代期间
$a
日志

[2016-05-19 07:22:12] local.DEBUG: App\Models\User Object
(
    [fillable:protected] => Array
        (
            [0] => first_name
            [1] => last_name
            [2] => email
            [3] => password
        )

    [hidden:protected] => Array
        (
            [0] => password
            [1] => remember_token
        )

    [dates:protected] => Array
        (
            [0] => deleted_at
        )

    [connection:protected] => 
    [table:protected] => 
    [primaryKey:protected] => id
    [perPage:protected] => 15
    [incrementing] => 1
    [timestamps] => 1
    [attributes:protected] => Array
        (
            [first_name] => Christian
            [last_name] => Giupponi
            [email] => christian.giupponi@example.com
            [password] => $2y$10$aW/rP7oHqH0/mj7RfYBR9OdvxRCEXiYLOVCIvfY1BHGNCuBNdcy/i
            [updated_at] => 2016-05-19 07:22:12
            [created_at] => 2016-05-19 07:22:12
            [id] => 1
        )

    [original:protected] => Array
        (
            [first_name] => Christian
            [last_name] => Giupponi
            [email] => christian.giupponi@example.com
            [password] => $2y$10$aW/rP7oHqH0/mj7RfYBR9OdvxRCEXiYLOVCIvfY1BHGNCuBNdcy/i
            [updated_at] => 2016-05-19 07:22:12
            [created_at] => 2016-05-19 07:22:12
            [id] => 1
        )

    [relations:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [appends:protected] => Array
        (
        )

    [guarded:protected] => Array
        (
            [0] => *
        )

    [dateFormat:protected] => 
    [casts:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [with:protected] => Array
        (
        )

    [morphClass:protected] => 
    [exists] => 1
    [wasRecentlyCreated] => 1
    [forceDeleting:protected] => 
)

问题在于
assignRole
elountituserrepository
类的一种方法。因此,如果
$a
App\Models\User
的实例,
$a->assignRole()
不存在

如果要通过存储库分配角色,必须调用存储库上的方法:

$this->userRepository->assignRole($adminRole);
前提是该方法将获得对实际
用户
模型的访问权,并为其分配角色。如果不是这样,您可以将实际的
模型
作为参数传递,并在方法中分配角色:

//pass both role and model to the repository
public function assignRole(Role $role, Model $user)
{
    //here assign the role to the user
}
因此,您的代码将是:

$a = $this->userRepository->create($admin);
$this->userRepository->assignRole($adminRole, $a);

EloquentUserRepository::创建什么
返回?它似乎是
illumb\Database\Query\Builder
的一个实例,它从错误中返回
App\Models\User
,它似乎试图在
illumb\Database\Query\Builder
上调用
assignRole
,因此可能在循环的某个迭代中出错了?在调用
assignRole
之前,是否可以尝试在每次迭代中记录
$a
的内容?即:
Log::debug(print_r($a,true))
使用logi更新了问题,请参见
assignRole
EloquentUserRepository
类的一种方法。因此,如果
$a
App\Models\User
的实例,
$a->assignRole()
不存在。或者您也有
App\Models\User::assignRole
方法?
//pass both role and model to the repository
public function assignRole(Role $role, Model $user)
{
    //here assign the role to the user
}
$a = $this->userRepository->create($admin);
$this->userRepository->assignRole($adminRole, $a);