I have three models Office, Desk, Reservation. An Office can have many Desks and a Desk belongs to an Office. A Desk can have many Reservations and a Reservation belongs to a Desk.
What I want to achieve now, is a query with such kind of result:
@Tray2 Maybe my question is a little bit missleading. My goal is to create some kind of month based overview which office has how much reservation and colorize these days accordingly. So that a user can decide if he wants to book a desk in an office which is already crowded.
To achieve this, do I have to make three queries? One for the offices, one for the desks, and one for the reservations of a month and merge them together? Or can I achieve this within one query?
To reserve a desk on a specific day is already implemented.
@wikorl A ok, no you can run that with a single query. My guess is tha you have the dates in your reservations table, that makes it a bit trickier, you need to fetch them first then which desk and which office.
$sql="SELECT t1.id,l.created_at,count(*) FROM t1 LEFT JOIN t2 ON t1.id=t2.t1_id LEFT JOIN t3 ON t2.id=t3.t2_id GROUP BY t1.id.t3.created_at ORDER BY t1.id,t3.created_at";
with your tables
t1 = offices
t2= desks
t3= reservations
your fields
t1.id -> t1.office
t3.created_at -> t3.reservation_date
and your relations
t1_id=office_id
t2_id=desk_id
Than run this query with DB::select($sql);
If any difficulty share your code and your question / the error you get.