$institutes = Institute::with(['address' => function($query){
$query->whereCityId(Input::get('city_id'));
$query->whereIn('area_id',Input::get('area_id'));
}])->get();
()提供的foreach 無效參數如何做laravel渴望加載多個約束?
$institutes = Institute::with(['address' => function($query){
$query->whereCityId(Input::get('city_id'));
$query->whereIn('area_id',Input::get('area_id'));
}])->get();
()提供的foreach 無效參數如何做laravel渴望加載多個約束?
它的工作,但它返回
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;
});
這個問題可能是這裏
$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);
它確實返回一個數組! –
是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();
你能在錯誤 – StateLess
闡述我想所有的機構與在city_id這是地址地址table和area_id也在地址表中匹配輸入條件 –
請查看此[link](https://laravel.com/docs/5.4/eloquent-relationships#querying-relations)。以下是關於查詢的更多信息關係。用於在哪裏放了。 –