[英]Laravel validation get value from a column and compare it to another table column
I have the following tables: 我有以下表格:
- companies
- users
- departments
- services
We have the following structure: 我们具有以下结构:
A user belongs to a company
A company has many departments
One department has multiple services.
The problem is that I want to validate the service department that has a specific company_id
flag using only the service ID. 问题是我想仅使用服务ID来验证具有特定company_id
标志的服务部门。
Database: 数据库:
company 公司
| id | name |
| 1 | Company 1 |
| 2 | Company 2 |
user 用户
| id | name | company_id |
| 1 | User 1 | 1 |
departments 部门
| id | name | company_id |
| 1 | Department 1 | 1 |
| 2 | Department 2 | 2 |
services 服务
| id | name | departments_id |
| 1 | Service 1 | 1 |
| 2 | Service 2 | 2 |
The endpoints looks like this: 端点看起来像这样:
Looged in user belongs to Company 1
迷迷糊糊的用户属于Company 1
$app->delete('/{id}', ['uses' => 'ServicesController@deleteService']);
Delete service method 删除服务方式
public function deleteService($id, Request $request)
{
$request['id'] = $id;
$this->validate($request, [
'id' => 'required|exists:services,id',
]);
$result = Service::deleteService($id, $this->user->company_id);
return response()->json($result);
}
The user of Company 1
should not be able to delete the Service 2
. Company 1
的用户不能删除Service 2
。 How can I achieve this? 我该如何实现?
Thanks. 谢谢。
最可监督的方式是编写自定义验证器
You can do it using policies
in Laravel, check the docs 您可以使用Laravel中的policies
进行操作,请检查文档
it will be something like this 会像这样
$request['id'] = $id;
$this->validate($request, [
'id' => 'required|exists:services,id',
]);
$this->authorize('delete', $id); // will check if user can delete the company as the needed logic.
$result = Service::deleteService($id, $this->user->company_id);
return response()->json($result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.