2017-04-13 65 views
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只適用於做查詢,並沒有操縱已返回集合點?

包括查詢

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); 
    } 

回答

0

你需要鏈get()方法來執行查詢,並得到一個集合。試試這個:

$all_locations = Service::getLocationsForService($service_id, 'all')->get(); 
+0

問題在於查詢以find($ id)結束;鏈接得到一切。 我只需要將查找移至where()? –

+0

@CraigWard你能顯示你的查詢嗎? –

+1

@CraigWard請顯示所有相關代碼。 find()會返回一個對象,而不是集合,但是你的代碼返回一個查詢生成器實例。 –