gpt4 book ai didi

php - Laravel 验证从列中获取值并将其与另一个表列进行比较

转载 作者:行者123 更新时间:2023-12-04 17:49:43 26 4
gpt4 key购买 nike

我有以下表格:

- companies
- users
- departments
- services

我们有以下结构:
A user belongs to a company A company has many departments One department has multiple services.
问题是我想验证具有特定 company_id 的服务部门仅使用服务 ID 的标志。

数据库:

公司
| id | name         |
| 1 | Company 1 |
| 2 | Company 2 |

用户
| id | name         | company_id |
| 1 | User 1 | 1 |

部门
| id | name         | company_id |
| 1 | Department 1 | 1 |
| 2 | Department 2 | 2 |

服务
| id | name      | departments_id |
| 1 | Service 1 | 1 |
| 2 | Service 2 | 2 |

端点如下所示:

登录用户属于 Company 1
$app->delete('/{id}', ['uses' => 'ServicesController@deleteService']);

删除服务方法
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);
}
Company 1的用户应该不能删除 Service 2 .
我怎样才能做到这一点?

谢谢。

最佳答案

你可以使用 policies在 Laravel 中,检查 docs

它会是这样的

    $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);

关于php - Laravel 验证从列中获取值并将其与另一个表列进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46074953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com