| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
|
| 7 |
class Tag extends Model |
| 8 |
{ |
| 9 |
protected $table = 'fs_taggables'; |
| 10 |
|
| 11 |
protected $fillable = ['tag_type', 'title', 'slug', 'description', 'settings', 'created_by']; |
| 12 |
|
| 13 |
protected $searchable = ['title', 'description']; |
| 14 |
|
| 15 |
public function setSettingsAttribute($settings) |
| 16 |
{ |
| 17 |
$this->attributes['settings'] = \maybe_serialize($settings); |
| 18 |
} |
| 19 |
|
| 20 |
public function getSettingsAttribute($value) |
| 21 |
{ |
| 22 |
return Helper::safeUnserialize($this->attributes['settings']); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Local scope to filter tags by search/query string |
| 27 |
* @param ModelQueryBuilder $query |
| 28 |
* @param string $search |
| 29 |
* @return ModelQueryBuilder |
| 30 |
*/ |
| 31 |
|
| 32 |
public function scopeSearchBy($query, $search) |
| 33 |
{ |
| 34 |
if ($search) { |
| 35 |
$fields = $this->searchable; |
| 36 |
$query->where(function ($query) use ($fields, $search) { |
| 37 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 38 |
|
| 39 |
foreach ($fields as $field) { |
| 40 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 41 |
} |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
return $query; |
| 46 |
} |
| 47 |
} |
| 48 |
|