Laravel 存储库和控制器
Laravel repository and controller
这是我的存储库
public function showInterface(int $id)
{
$role = $this->model->find($id);
$rolePermissions = Permission::join("role_has_permissions","role_has_permissions.permission_id","=","permissions.id")
->where("role_has_permissions.role_id",$id)
->get();
return $role && $rolePermissions;
}
这是我的控制器
public function show($id)
{
$role=$this->repository->showInterface($id);
return view('roles.show',compact('role'));
}
问题是我应该在 Controller 中压缩 $role 和 $rolePermissions,我应该怎么做?
我认为你应该将其更改为:
public function showInterface(int $id)
{
$role = $this->model->find($id);
$rolePermissions = Permission::join("role_has_permissions","role_has_permissions.permission_id","=","permissions.id")
->where("role_has_permissions.role_id",$id)
->get();
return [$role, $rolePermissions];
}
和
public function show($id)
{
[$role, $rolePermissions] = $this->repository->showInterface($id);
return view('roles.show',compact('role', 'rolePermissions'));
}
这是我的存储库
public function showInterface(int $id)
{
$role = $this->model->find($id);
$rolePermissions = Permission::join("role_has_permissions","role_has_permissions.permission_id","=","permissions.id")
->where("role_has_permissions.role_id",$id)
->get();
return $role && $rolePermissions;
}
这是我的控制器
public function show($id)
{
$role=$this->repository->showInterface($id);
return view('roles.show',compact('role'));
}
问题是我应该在 Controller 中压缩 $role 和 $rolePermissions,我应该怎么做?
我认为你应该将其更改为:
public function showInterface(int $id)
{
$role = $this->model->find($id);
$rolePermissions = Permission::join("role_has_permissions","role_has_permissions.permission_id","=","permissions.id")
->where("role_has_permissions.role_id",$id)
->get();
return [$role, $rolePermissions];
}
和
public function show($id)
{
[$role, $rolePermissions] = $this->repository->showInterface($id);
return view('roles.show',compact('role', 'rolePermissions'));
}