I'm using Query Builder to get results from multiple joins tables with this query :
$products = DB::table('products as p')
->select(
'p.id',
'p.name',
'p.reference',
'p.price',
'i.path',
'i.name'
)
->join('products_r_images as pri', 'pri.product_id', '=', 'p.id')
->join('images as i', 'i.id', '=', 'prd.image_id')
->get();
One product can have multiples images associated.
With the above query, I obtains this result :
[
{
id: 3,
name: "Test",
reference: "ref-test",
price: 123,
image_path: "product/product-3/",
image_name: "product_1.jpg"
},
{
id: 3,
name: "Test",
reference: "ref-test",
price: 123,
image_path: "product/product-3/",
image_name: "product_2.jpg"
}
]
As you can see, two rows are returns for one product, while I would like to have documents datas returns in one row with something like that :
[
{
product_id: 3,
name: "Test",
reference: "ref-test",
price: 123,
image_path: "product/product-3/",
image_name:
[
"product_1.jpg", "product_2.jpg"
]
}
]
Is there a way to do that directly with Query Builder, or another treatment is needed ?
Eloquentmodels set up for these tables?Productmodel returninghasMany(App\Images)? If it is, no I didn't, because for this query I only useQuery Builder.belongsToManynothasMany