简体   繁体   English

laravel - 如何检查子子子关系是否存在

[英]laravel - how to check if sub sub sub relation exists

My controller is receiving an AgentId我的控制器正在接收 AgentId

public function save(Int $agent_id, Request $request)

So now I need to check if current user has permission to handle this agent_id.所以现在我需要检查当前用户是否有权处理这个 agent_id。

The problem is that they are not directly connect with a relation.问题是它们没有直接与关系连接。

The path between these models is:这些模型之间的路径是:

User -> hasMany Workspace -> hasMany Inventory -> hasOne Agent

So the question is: is there an easier/cleaner way to check if the User has connection with this Agent?所以问题是:有没有更简单/更干净的方法来检查用户是否与此代理有连接?

What I'm trying so far is to loop all items for each relation level.到目前为止,我正在尝试为每个关系级别循环所有项目。

Agent model has a relation with a User , because you can't create a child table row without its parent data. Agent模型与User有关系,因为您不能在没有父数据的情况下创建子表行。 You can reach User table with join method, like this :您可以使用 join 方法访问 User 表,如下所示:

$q = Agent::select('agents.*')
    ->where('id', $agent_id)
    ->join('inventories', 'inventories.id', '=', 'agents.inventory_id')
    ->join('workspaces', 'workspaces.id', '=', 'inventories.workspace_id')
    ->join('users', 'users.id', '=', 'workspaces.workspace_id')
    ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM