Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: roman-kozin
- * Date: 03.07.18
- * Time: 12:31
- */
- namespace App\Traits;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Model;
- /**
- * Trait Search
- * @package App\Traits
- * @property array searchFields
- */
- trait Search
- {
- protected $searchFields = ['title', 'title_en', 'Taggs', 'Taggs_en'];
- /**
- * @param Model|Builder $model
- * @param null $q
- * @param array $noFields
- * @return array
- */
- public function findForSearch($model, $q = null, $noFields = [])
- {
- if ($noFields) {
- $this->searchFields = array_diff($this->searchFields, $noFields);
- }
- foreach ($this->searchFields as $i => $searchField) {
- $model = !$i
- ? $model->where($searchField, 'like', "%{$q}%") :
- $model->orWhere($searchField, 'like', "%{$q}%");
- }
- return $model->get()->toArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment