1
私はEloquentがコレクションを返すと仮定していたので、以下は動作します。 返された雄弁のコレクションを操作する
$all_locations = Service::getLocationsForService($service_id, 'all');
$chosen_locations = Service::getLocationsForService($service_id, $locations);
$diff = $chosen_locations->diff($all_locations)
しかし、私はちょうど得る
Call to undefined method Illuminate\Database\Query\Builder::diff()
クエリを作成し、既に返されたコレクションを操作していない時点でのこれらhttps://laravel.com/docs/5.4/collections#available-methodsのみ利用できますか?
はあなたがチェーンにクエリを実行し、コレクションを取得するget()
方法を必要とするクエリ
public static function getLocationsForService($service_id, $locations)
{
if($locations[0] == 'all') { $locations[0] = ''; }
return Service::with(['types', 'contacts', 'locations.datetimes' =>function($q) use($service_id){
$q->where('service_id', $service_id);
}, 'conditions', 'locations' => function($query) use($locations) {
$ran = false;
foreach($locations as $location)
{
if(!$ran)
{
$query->Where('town', 'like', '%'.$location.'%')
->orWhere('city', 'like', '%'.$location.'%');
}
else
{
$query->orWhere('town', 'like', '%'.$location.'%')
->orWhere('city', 'like', '%'.$location.'%');
}
$ran = true;
$query->Where('published', 1);
}
}])->find($service_id);
}
問題は、find($ id)で終了します。連鎖はすべてを引き出す。 findをwhere()に移動するだけでいいですか? –
@CraigWardあなたの質問を表示できますか? –
@CraigWardすべての関連コードを表示してください。 'find()'はコレクションではなくオブジェクトを返しますが、コードはQuery Builderインスタンスを返します。 –