If you want nested results like eloquent, why not use eloquent?
Dec 30, 2017
4
Level 12
Active results like Eloquent "with" in query builder
Hello, I'm trying to get database records as same as Eloquent "with" so
for example my query builder query return the following data
$comments = \DB::table('comments as T1')
->select('T1.product_id','rating')
->where('T1.shop_name', '=', "$shop")
->join('products as pr', 'pr.product_id', '=', 'T1.product_id')
->distinct()
->paginate(10, [ 'T1.product_id' ]);
data": [
{
"product_id": "465549557783",
"rating": 4
},
{
"product_id": "465549557783",
"rating": 5
},
{
"product_id": "465549557783",
"rating": 3
},
{
"product_id": "465549557783",
"rating": 2
},
{
"product_id": "465549557783",
"rating": 1
},
{
"product_id": "478759485463",
"rating": 5
},
{
"product_id": "478759485463",
"rating": 4
},
{
"product_id": "478759485463",
"rating": 3
}
but I want the id to be unique so how can I achieve a results like this one ?
data": [
{
"product_id": "465549557783",
{
"rating": 4,
"rating": 5,
"rating": 3,
"rating": 2,
"rating": 1,
}
},
{
"product_id": "478759485463",
{
"rating": 5,
"rating": 4,
"rating": 3
}
},
Is it possible ?
Level 36
@ahmadissa You might try the "has("relationship")" method off your model. That will filter down to items that have that relation.
1 like
Please or to participate in this conversation.