|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Javaabu\Cms\Models; |
| 4 | + |
| 5 | +use Javaabu\Helpers\AdminModel\AdminModel; |
| 6 | +use Javaabu\Helpers\AdminModel\IsAdminModel; |
| 7 | +use Javaabu\Activitylog\Traits\LogsActivity; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Javaabu\Helpers\Traits\HasSlug; |
| 10 | +use Javaabu\Translatable\Contracts\Translatable; |
| 11 | +use Javaabu\Translatable\JsonTranslatable\IsJsonTranslatable; |
| 12 | + |
| 13 | +class Tag extends Model implements AdminModel |
| 14 | +// , Translatable |
| 15 | +{ |
| 16 | + use IsAdminModel; |
| 17 | + use LogsActivity; |
| 18 | +// use HasSlugForTranslatables; |
| 19 | +// use IsJsonTranslatable; |
| 20 | + |
| 21 | + /** |
| 22 | + * The attributes that would be logged |
| 23 | + * |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + protected static array $logAttributes = ['*']; |
| 27 | + |
| 28 | + /** |
| 29 | + * Changes to these attributes only will not trigger a log |
| 30 | + * |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + protected static array $ignoreChangedAttributes = ['created_at', 'updated_at']; |
| 34 | + |
| 35 | + /** |
| 36 | + * The attributes that are mass assignable. |
| 37 | + * |
| 38 | + * @var array |
| 39 | + */ |
| 40 | + protected $fillable = ['name']; |
| 41 | + |
| 42 | + /** |
| 43 | + * The attributes that are cast to native types. |
| 44 | + * |
| 45 | + * @var array |
| 46 | + */ |
| 47 | + protected $casts = [ |
| 48 | + 'name' => 'string', |
| 49 | + ]; |
| 50 | + |
| 51 | + /** |
| 52 | + * The attributes that are searchable. |
| 53 | + * |
| 54 | + * @var array |
| 55 | + */ |
| 56 | + protected $searchable = ['name',]; |
| 57 | + |
| 58 | + public function getTranslatables(): array |
| 59 | + { |
| 60 | + return [ |
| 61 | + 'name', |
| 62 | + ]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get the admin url attribute |
| 67 | + */ |
| 68 | + public function getAdminUrlAttribute(): string |
| 69 | + { |
| 70 | + return route('admin.tags.show', $this); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Sanitize the name |
| 75 | + * |
| 76 | + * @param string $text |
| 77 | + * @param string $special_char |
| 78 | + * @return string |
| 79 | + */ |
| 80 | + public static function sanitizeName(string $text, string $special_char = ' '): string |
| 81 | + { |
| 82 | + return str($text)->slug($special_char); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the slug attribute |
| 87 | + * |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + public function getSlugAttribute(): string |
| 91 | + { |
| 92 | + return str($this->translate('name', 'en'))->slug(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Has slug scope |
| 97 | + * |
| 98 | + * @param $query |
| 99 | + * @param $slug |
| 100 | + * @return mixed |
| 101 | + */ |
| 102 | + public function scopeHasSlug($query, $slug) |
| 103 | + { |
| 104 | + // replace - with spaces |
| 105 | + $actual_slug = str($slug)->slug(' '); |
| 106 | + |
| 107 | + return $this->whereName($actual_slug); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get localized admin URL |
| 112 | + * |
| 113 | + * @return mixed|string |
| 114 | + */ |
| 115 | + public function getAdminLocalizedUrl() |
| 116 | + { |
| 117 | + return $this->admin_url; |
| 118 | + } |
| 119 | +} |
0 commit comments