【发布时间】:2016-09-15 21:54:37
【问题描述】:
我创建了一个EloquentUserRepository 以保持我的代码简洁和可扩展。
这是我的EloquentUserRepository:
namespace App\Repositories\User;
use App\Models\Role;
use App\Repositories\EloquentRepository;
class EloquentUserRepository extends EloquentRepository implements UserContract
{
/**
* Get class name
*
* @return string
*/
protected function getModelName()
{
return 'App\Models\User';
}
/**
* Check if user has the given role.
* Could be a string or a collection
*
* @param $role
*/
public function hasRole($role)
{
if (is_string($role)) {
return $this->roles->contains('name', $role);
}
// This will remove from the roles all the roles that do not match the given one.
// If the result is empty the user do not have that role.
return $role->intersect($this->roles)->count();
}
/**
* Assign the role to the user
*
* @param Role $role
*/
public function assignRole(Role $role)
{
return $this->model->roles()->save($role);
}
}
但是当我在我的播种机中使用它时:
use App\Models\Role;
use App\Models\User;
use App\Repositories\User\EloquentUserRepository;
use Illuminate\Database\Seeder;
class AdminSeeder extends Seeder
{
/**
* @var EloquentUserRepository
*/
private $userRepository;
/**
* AdminSeeder constructor.
*
* @param EloquentUserRepository $userRepository
*/
public function __construct(EloquentUserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* Run the database seeds.
*
*/
public function run()
{
$admins = [
[
'first_name' => 'Christian',
'last_name' => 'Giupponi',
'email' => 'christian.giupponi@example.com',
'password' => 'sviluppo'
]
];
$adminRole = Role::where('name', 'admin')->get()->first();
foreach($admins as $admin){
$a = $this->userRepository->create($admin);
$a->assignRole($adminRole);
}
}
}
我明白了:
BadMethodCallException] 调用未定义的方法 Illuminate\Database\Query\Builder::assignRole()
这是create 方法:
/**
* Create new resource
*
* @param array $data
* @return mixed
*/
public function create(array $data)
{
return $this->model->create($data);
}
如果我dd 我得到User 模型:
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 中的Log 迭代:
[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] =>
)
【问题讨论】:
-
EloquentUserRepository::create返回什么?这似乎是Illuminate\Database\Query\Builder的一个实例 -
它返回
App\Models\User -
从错误看来,它试图在
Illuminate\Database\Query\Builder上调用assignRole,所以也许在循环的迭代中出现了问题?您可以尝试在assignRole调用之前的每次迭代中记录$a的内容吗?即:Log::debug( print_r($a, true) ); -
用日志更新了问题
-
我看到
assignRole是EloquentUserRepository类的一个方法。因此,如果$a是App\Models\User的一个实例,则$a->assignRole()不存在。或者你也有App\Models\User::assignRole方法?
标签: php laravel laravel-5 laravel-5.2