操纵返回的雄辩集合

时间:2017-04-13 09:39:40

标签: laravel laravel-5

我假设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);
    }

1 个答案:

答案 0 :(得分:0)

您需要链接get()方法来执行查询并获取集合。试试这个:

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