Since the A.I. misunderstood me. I want to to see the admin.server_id placed at 3. admin.server_id from the outer query. From the admin list each admin has a server_id depends on the server it belongs to.
Pass value into the with() function
Hi! Is it possible to access data inside of a with() query?
So in the following example there are ServerGroups. A ServerGroup has many Servers. The Servers has many Admins. The Admins has many Names.
I can load the Admins from a ServerGroup. It means all Admins belongs to all Servers owned by a ServerGroup.
return $serverGroup->admins()->paginate();
I can populate the admins with the server.
return $serverGroup->admins()->with('server')->paginate();
Same scenario with names.
return $serverGroup->admins()->with('server')->with('names')->paginate();
In this case all the admins printed well with associated server and names.
{
"id": 1,
"steam_id": "MY_STEAM_ID",
"server_id": 3,
"flags": "abc",
"created_at": null,
"updated_at": null,
"laravel_through_key": 2,
"server": {
"id": 3,
"label": "MY_SERVER",
"ip_address": "127.0.0.1",
"port": null,
"server_group_id": 2,
"created_at": null,
"updated_at": null,
"deleted_at": null
},
"names": [
{
"id": 1,
"steam_id": "MY_STEAM_ID",
"name": "XXX",
"server_id": 3,
"created_at": "2023-05-19 22:54:20",
"updated_at": "2023-05-19 22:54:34"
},
{
"id": 1,
"steam_id": "MY_STEAM_ID",
"name": "Blah",
"server_id": 4,
"created_at": "2023-05-19 22:54:20",
"updated_at": "2023-05-19 22:54:34"
}
]
},
In this respone there is an admin as you can admin of the server 3. Loaded with the server and the names. But I can see all the names the admin used on another servers.
I want to filter that names by server id. Example: If the admin is belongs to Server1 I only want to show the names used by the admin on the Server1.
return $serverGroup->admins()->with('server')->with(['names' => fn ($q2) use ($q) => $q2->where('server_id', 3)])->paginate();
If I set he filter to server_id 3 the only name is printed fine. I think it can be done with DB:raw('admins.server_id') instead of 3. But since the query is a prequery it cannot see that table. Maybe I can access that value from the $q variable. The main query.
do you not have a names relationship on the server model?
I was expecting your query to be like;
return $serverGroup->admins()->with('server.names')->paginate();
Please or to participate in this conversation.