public function scopeBySearchPhrase($query, $phrase) { $phrase = trim($phrase); if (!$phrase) { return $query; } // by city name $citiesIds = City::active()->searchByTitle($phrase)->lists('id'); if ($citiesIds) { return $query->byCity($citiesIds); } // by metro station $officesIds = Office::active()->searchByMetro($phrase)->lists('id'); if ($officesIds) { return $query->whereIn('id', $officesIds); } // by address return $query->byAddress($phrase); }