Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

28a.gdev's avatar

How does Eloquent know which client I need?

I'm planning how i will implement the below using Eloquent & Laravel version 8:

Users have many clients, Clients only have one user.

Each client has many appointments, each appointment only has one client.

Say I'm User 1 and i click on client 17 and query to show their appointments. How do i set it up so that it only shows user 1 > client 17 > appointments and not pull user 2 > client 17 > appointments ?

its very hard for me to word this simply as i don't fully understand it myself so apologies in advance if this makes no sense. essentially I have users who each have clients and those clients have appointments but each users data needs to be separate to every other user.

0 likes
5 replies
28a.gdev's avatar

I should of also mentioned im new here and a beginner, i have searched extensively through forums/stackoverflow and github projects but My question is more basic and all other scenarios i've found confuse me more

ghiath.dev's avatar
Level 51

It sounds that you are looking for ensuring that every user sees his own clients and the data belonged to it.

This is quite easy using policies:

https://laravel.com/docs/8.x/authorization#creating-policies

Relations you need:

  1. User has many clients: one to many (clients table should has user_id)
  2. Client has many appointments: one to many (appointments table has client_id)
28a.gdev's avatar

@ghiath-dev thank you for the quick respsonse,

as it stands, i have a clients table with a user_id col and im going to then make an appointments table which should have client_id and a user_id? or just client_id?

then i can look to set some policies to control who sees what?

Thanks for this i will look into it further

ghiath.dev's avatar

@28a.gdev I think there is no need to add the user_id to appointments table unless you need to query appointments from User model directly. (even that in case you can access it through clients)

1 like

Please or to participate in this conversation.