| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
|
| 7 |
/** |
| 8 |
* @property int $id |
| 9 |
* @property string|null $slug |
| 10 |
* @property string|null $title |
| 11 |
* @property string|null $taxonomy_name |
| 12 |
* @property string|null $description |
| 13 |
* @property mixed $settings |
| 14 |
*/ |
| 15 |
class Term extends Model |
| 16 |
{ |
| 17 |
protected $table = 'fcom_terms'; |
| 18 |
|
| 19 |
protected $guarded = ['id']; |
| 20 |
|
| 21 |
protected $fillable = [ |
| 22 |
'parent_id', |
| 23 |
'taxonomy_name', |
| 24 |
'slug', |
| 25 |
'title', |
| 26 |
'description', |
| 27 |
'settings' |
| 28 |
]; |
| 29 |
|
| 30 |
protected $searchable = [ |
| 31 |
'title', |
| 32 |
'description', |
| 33 |
'slug' |
| 34 |
]; |
| 35 |
|
| 36 |
public static function boot() |
| 37 |
{ |
| 38 |
parent::boot(); |
| 39 |
|
| 40 |
static::deleting(function ($term) { |
| 41 |
$term->posts()->detach(); |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
public function scopeSearchBy($query, $search) |
| 46 |
{ |
| 47 |
if ($search) { |
| 48 |
$fields = $this->searchable; |
| 49 |
$query->where(function ($query) use ($fields, $search) { |
| 50 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 51 |
foreach ($fields as $field) { |
| 52 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 53 |
} |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
return $query; |
| 58 |
} |
| 59 |
|
| 60 |
public function posts() |
| 61 |
{ |
| 62 |
return $this->belongsToMany(Feed::class, 'fcom_term_feed', 'term_id', 'post_id') |
| 63 |
->withoutGlobalScopes(); |
| 64 |
} |
| 65 |
|
| 66 |
public function getSettingsAttribute($value) |
| 67 |
{ |
| 68 |
$settings = Utility::safeUnserialize($value); |
| 69 |
|
| 70 |
if (!$settings) { |
| 71 |
$settings = []; |
| 72 |
} |
| 73 |
|
| 74 |
return $settings; |
| 75 |
} |
| 76 |
|
| 77 |
public function setSettingsAttribute($value) |
| 78 |
{ |
| 79 |
$this->attributes['settings'] = maybe_serialize($value); |
| 80 |
} |
| 81 |
|
| 82 |
public function base_spaces() |
| 83 |
{ |
| 84 |
return $this->belongsToMany(BaseSpace::class, 'fcom_meta', 'object_id', 'meta_key') |
| 85 |
->wherePivot('object_type', 'term_space_relation') |
| 86 |
->withoutGlobalScopes(); |
| 87 |
} |
| 88 |
} |
| 89 |
|