2016-12-25 19 views
1

私は2つの異なるlaravelクエリを持っています。私は最初のクエリに基づいてwhereNotInで2番目のクエリを使いたいと思います。 whereNotInはまだデータが表示されているため動作していないようです。laravel whereNotIn not working

この最初のクエリ

$detailservice = DetailServiceOrder::leftJoin('services', 'detail_service_orders.service_id', '=', 'services.id') 
    ->select('detail_service_orders.*', 
     'services.service_name') 
    ->where('sales_order_id', $id) 
    ->get(); 

この2番目のクエリ

foreach ($detailservice as $data) {    
     $service[] = Service::whereNotIn('id', [$data->service_id])->get();  
    } 

    dd($service); 

とここに私のdd($service)結果サンプル

#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" 
    ] 

とここに私のdd($detailservice)結果サンプル

#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" 
    ] 

上記のdd($service)の結果は、2番目のクエリでフィルタリングされたため表示されません。

+0

'DD($のdetailservice)何が;'ショー? –

+0

こんにちは、私は私の質問に 'dd($ detailservice)'を追加しました – rafitio

+0

ありがとうございました。以下の答えを確認してください。 –

答えて

1

コードは正常に動作します。まず、IDが1でない結果を取得し(2を取得)、IDが2でない結果を返します(IDが1のオブジェクトを返します)。

あなたはIDの代わりにforeach()のこれを使用する、1または2でない結果を取得したい場合:

$services = Service::whereNotIn('id', $data->pluck('service_id'))->get(); 
+0

こんにちは、それは動作していますが、私は別の問題を抱えています。結果が '#items:array:12 [▼ 0 =>]であるため、ID 1またはID 2が表示されませんでした。 1 1 => 1 2 => 1 3 => 1 4 => 1 5 => 1 6 => 2 7 => 2 8 => 2 9 => 2 10 => 2 11 => 2 ] – rafitio

+0

あなたのコメントから質問を理解できませんでした。あなたはそれを言い換えてください。 –

+0

現在、 '$ data-> pluck( 'service_id')'をデバッグするとき、 'service_id'の値が' 1'と '2'(上のコメントのように)であるため、サービスを表示していません。つまり、ID 1またはID 2ではなくID 1とID 2をフィルタリングしないため、すべてのデータが表示されませんでした。 CMIIW – rafitio