2014-10-16 1 views
0

$item_ids = array('1', '17'); 중 하나를 가지고있는 모든 Shop을 얻으려고합니다.어디에서 예상대로 작동하지 않습니다

그러나 아래 코드는 내가 기대했던대로 그렇게하지 않습니다. 단지 모든 상점을 나에게 건네줍니다.

$shops = Shop::whereHas('items', function($query) use ($item_ids) { 

        $query->where('items.id', '=', $item_ids[0]); 

        foreach(array_slice($item_ids, 1) as $item_id) { 
         $query->orWhere('items.id', '=', $item_id); 
        } 
       }) 
       ->get(array('shops.id', 'shops.shop_name', 'shops.lat', 'shops.lng')); 

내가는 지정된 Item들 중 하나를 사용하여 Shop의를받을 수 있나요?

답변

1

당신은 오히려 사용해야

$shops = Shop::whereHas('items', function($query) use ($item_ids) {  
    $query->whereIn('id', $items_ids); 
})->get(); 

또는

$shops = Shop::whereHas('items', function($query) use ($item_ids) {  
    $query->whereIn('id', $items_ids); 
})->select('shops.id', 'shops.shop_name', 'shops.lat', 'shops.lng')->get(); 
관련 문제