【发布时间】:2020-08-03 11:38:31
【问题描述】:
我有两个卫兵
Admin
User
我还创建了控制器,用户可以在其中管理自己的数据,管理员也可以管理用户数据。所以我创建了两个控制器
Controllers
Admin
EducatonBackgroundController
User
EducationBackgroundController
在 User/EducationBackgroundController 中,我有这个函数可以获取当前登录用户的教育背景并显示在用户视图上
public function index(Education $education)
{
try {
$educations = $education->where('user_id',$this->userLoggedID())->with('organization','program','country','city')->get();
return view('users.education.index',compact('educations'));
}
catch (Exception $e) {
abort(404);
}
}
在 Admin/EducationBackgroundController 我有这个功能,它可以获取所有用户的教育背景并显示在管理视图上
public function index(Education $education)
{
try {
$educations = $education->with('organization','program','country','city','user')->get();
return view('admin.users.education.index',compact('educations'));
}
catch (Exception $e) {
abort(404);
}
}
从观察来看,这些功能相似,但在视图返回和数据获取方面有所不同。
那么我怎样才能创建一个可供管理员和用户警卫使用的控制器,而不是两个警卫的重复控制器和视图。
【问题讨论】:
标签: laravel laravel-6 laravel-7 laravel-authorization laravel-authentication