Either add the same query to a where() or add a ->havingRaw('task_action IS NOT NULL')
Sep 14, 2022
5
Level 3
limit results based on the existence of a sub query column
Hi, can someone help me, I just need to limit the results based on the relation sub query column (task_action).
here's my current query
$orders = Order::select('id')
->with(['templateIndexOne' => function ($q) {
$q->addSelect(['task_action' => Task::select('action')
->whereColumn('mapping_id', 'template_mappings.id')
->latest('task_id')
->limit(1)
]);
}])
->has('templateIndexOne')
->where('user_id', auth()->id())
->paginate();
return $orders;
I only need to display all Orders where templateIndexOne.task_action = makejpglocal.
"data":[
{
"id":1452824,
"template_index_one":{
"id":138657,
"user_id":17,
"template_id":163409,
"template_index":1,
"match_item_only":185983536,
"match_product_code":null,
"match_variation":null,
"copy_template_index":null,
"copy_template_mapping_id":null,
"functions":null,
"created_at":"2022-09-09T10:36:15.000000Z",
"updated_at":"2022-09-09T10:36:15.000000Z",
"match_type":null,
"match_value":null,
"order_item_id":1452824,
"task_action":"findapproved"
}
},
{
"id":1452825,
"template_index_one":{
"id":138672,
"user_id":17,
"template_id":163408,
"template_index":1,
"match_item_only":185957465,
"match_product_code":null,
"match_variation":null,
"copy_template_index":null,
"copy_template_mapping_id":null,
"functions":null,
"created_at":"2022-09-12T06:07:23.000000Z",
"updated_at":"2022-09-12T06:07:23.000000Z",
"match_type":null,
"match_value":null,
"order_item_id":1452825,
"task_action":"makejpglocal"
}
}
],
Expected Result:
"data":[
{
"id":1452825,
"template_index_one":{
"id":138672,
"user_id":17,
"template_id":163408,
"template_index":1,
"match_item_only":185957465,
"match_product_code":null,
"match_variation":null,
"copy_template_index":null,
"copy_template_mapping_id":null,
"functions":null,
"created_at":"2022-09-12T06:07:23.000000Z",
"updated_at":"2022-09-12T06:07:23.000000Z",
"match_type":null,
"match_value":null,
"order_item_id":1452825,
"task_action":"makejpglocal"
}
}
],
Level 102
Please or to participate in this conversation.