Reputation: 568
I have two different laravel query and i want to use second query with whereNotIn
based on first query. but whereNotIn
seems not working because the data still shown.
this the first query
$detailservice = DetailServiceOrder::leftJoin('services', 'detail_service_orders.service_id', '=', 'services.id')
->select('detail_service_orders.*',
'services.service_name')
->where('sales_order_id', $id)
->get();
this the second query
foreach ($detailservice as $data) {
$service[] = Service::whereNotIn('id', [$data->service_id])->get();
}
dd($service);
and here my dd($service)
result sample
#attributes: array:4 [▼
"id" => 2
"service_name" => "Mail Service"
"created_at" => "2016-11-26 02:15:24"
"updated_at" => "2016-11-26 02:15:24"
]
#attributes: array:4 [▼
"id" => 1
"service_name" => "Network Maintenance"
"created_at" => "2016-11-15 07:00:45"
"updated_at" => "2016-11-15 07:00:45"
]
and here my dd($detailservice)
result sample
#attributes: array:10 [▼
"sales_order_id" => 6
"service_id" => 1
"order_type" => "add"
"select_plan" => "test 1"
"qty" => 2
"unit_price" => 200.0
"note" => "testing 1"
"updated_at" => "2016-12-22 01:34:00"
"created_at" => "2016-12-22 01:34:00"
"service_name" => "Network Maintenance"
]
#attributes: array:10 [▼
"sales_order_id" => 6
"service_id" => 2
"order_type" => "change"
"select_plan" => "test 2"
"qty" => 3
"unit_price" => 400.0
"note" => "testing 2"
"updated_at" => "2016-12-22 01:34:00"
"created_at" => "2016-12-22 01:34:00"
"service_name" => "Mail Service"
]
the above dd($service)
result should not shown because it was filtered on second query.
Upvotes: 1
Views: 1344
Reputation: 163748
Your code works just fine. First, it gets results where ID is not 1 (so, it gets 2) and then it returns results where ID is not 2 (returns object where ID is 1).
If you want to get results where ID is not 1 or 2, use this instead of foreach()
:
$services = Service::whereNotIn('id', $data->pluck('service_id'))->get();
Upvotes: 1