2017-08-24 120 views
0
$institutes = Institute::with(['address' => function($query){ 
       $query->whereCityId(Input::get('city_id')); 
       $query->whereIn('area_id',Input::get('area_id')); 
       }])->get(); 

()提供的foreach 無效參數如何做laravel渴望加載多個約束?

+0

你能在錯誤 – StateLess

+0

闡述我想所有的機構與在city_id這是地址地址table和area_id也在地址表中匹配輸入條件 –

+0

請查看此[link](https://laravel.com/docs/5.4/eloquent-relationships#querying-relations)。以下是關於查詢的更多信息關係。用於在哪裏放了。 –

回答

0

它的工作,但它返回

address: null 

只要條件匹配。但它會返回所有機構。或者我們可以使用收集過濾whereHas

$institutes = Institute::whereHas('address', function ($query) use ($city_id) { 
        $query->whereCityId($city_id); 
       })->get(); 

$institutes = Institute::with(['address' => function ($query) use ($city_id) { 
        $query->whereCityId($city_id); 
       }])->get()->filter(function ($institute){ 
        return $institute->address != null; 
       }); 
0

這個問題可能是這裏

$query->whereIn('area_id',Input::get('area_id')); 

儘量保證Input::get('area_id')實際上返回數組:

$area = Input::get('area_id') ?: []; 
$query->whereIn('area_id',is_string($area) ? json_decode($area, true) : $area); 
+0

它確實返回一個數組! –

0

是whereCityId的模型的範圍?文檔僅包含與模型鏈接的範圍示例。

嘗試用列名替換範圍並嘗試。前替換city_id

$institutes = Institute::with(['address' => function($query){ 
        $query->where('city_id',Input::get('city_id')); 
        $query->whereIn('area_id',Input::get('area_id')); 
        }])->get();