| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
class Product extends Model |
| 6 |
{ |
| 7 |
protected $table = 'fs_products'; |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* The attributes that are mass assignable. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $fillable = [ |
| 16 |
'source_uid', |
| 17 |
'title', |
| 18 |
'description', |
| 19 |
'settings', |
| 20 |
'source', |
| 21 |
'created_by' |
| 22 |
]; |
| 23 |
|
| 24 |
protected $searchable = ['title', 'description']; |
| 25 |
|
| 26 |
/** |
| 27 |
* Local scope to filter products by search/query string |
| 28 |
* @param ModelQueryBuilder $query |
| 29 |
* @param string $search |
| 30 |
* @return ModelQueryBuilder |
| 31 |
*/ |
| 32 |
|
| 33 |
public function scopeSearchBy($query, $search) |
| 34 |
{ |
| 35 |
if ($search) { |
| 36 |
$fields = $this->searchable; |
| 37 |
$query->where(function ($query) use ($fields, $search) { |
| 38 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 39 |
|
| 40 |
foreach ($fields as $field) { |
| 41 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 42 |
} |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
return $query; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
} |
| 51 |
|