我创建了一个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]调用未定义的方法 照亮\数据库\查询\生成器:: 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
}
编辑:
Log
$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] =>
)
答案 0 :(得分:1)
问题是assignRole
是EloquentUserRepository
类的方法。因此,如果$a
是App\Models\User
的实例,则$a->assignRole()
不存在。
如果要通过存储库分配角色,则必须在存储库中调用该方法:
$this->userRepository->assignRole($adminRole);
前提是该方法可以访问实际的User
模型并将角色分配给它。如果不是这种情况,您可以将实际的Model
作为参数传递,并在方法中指定角色:
//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);