eager load the relationship
$customers =Customer::with('orders')->get();
and then each customer will have a nested collection of their orders
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
So, pardon me not having complete code yet because this is somewhat hypothetical.
So, if I have a query in my Controller that looks up a list of Customers from the Customers table, with various fields like name, email, etc. and it shows in a view with something like:
@foreach ($customers as $customer)
<div>$customer->name</div>
<div>$customer->email</div>
<div>$customer->phone</div>
<div>$customer->countoforders</div> (from the Orders Table)
@endforeach
Then, I want to create a Bootstrap tooltip for "countoforders" so that when I hover over the number it pops up a tooltip with a list of the orders for that customer.
I'm not asking for help with coding the tooltip.
I'm asking, what's the best way to query the orders table based on the current record inside the foreach.
I feel like if I do it within the controller, it won't know how to only give the orders for each customer, but I'm sure I'm missing something.
Or is there a way to return a subset of records for the orders and simply call them inside the foreach?
Please or to participate in this conversation.