You could do something like
$truck->customers->map(function($customer){
return $customer->name . implode(' ', $customer->products->pluck('name')->toArray());
});
Depending on how many items you have this might work.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
So I have three tables - products, customers and trucks. Each row in the product table is unique and only has a maximum quantity of one, so has a 'sold/instock' column.
Products are assigned to customers and then assigned to a truck for transportation. Many products for different customers can be loaded onto one truck.
I am creating a show truck page, each page should list all the agents that have products on that truck, then underneath the individual products.
Something like;
Customer 1 Product 1 Product 2 Product 5 Product 6
Customer 2 Product 3 Product 4 Product 7
I am thinking I have to do some kind of loop within a loop?
foreach customers where truck id =1 foreach products where truckid = 1 and customerid = 1
I have also been playing about with groupby on SQL but haven't had much success implementing anything into laravel. Could someone point me in the right direction for this?
Thanks in advance
Please or to participate in this conversation.