| 1 |  |  | <?php namespace Arcanesoft\Blog\Models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Arcanedev\Localization\Traits\HasTranslations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Illuminate\Database\Eloquent\SoftDeletes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Facades\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class     Category | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package  Arcanesoft\Blog\Models | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author   ARCANEDEV <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @property  int             id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @property  string          name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @property  string          slug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property  \Carbon\Carbon  created_at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property  \Carbon\Carbon  updated_at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property  \Carbon\Carbon  deleted_at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @property  \Illuminate\Database\Eloquent\Collection  posts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class Category extends AbstractModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      |  Traits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     use SoftDeletes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         HasTranslations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      |  Properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * The database table used by the model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected $table    = 'categories'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * The attributes that are mass assignable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected $fillable = ['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * The attributes that should be mutated to dates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected $dates    = ['deleted_at']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * The attributes that should be cast to native types. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     protected $casts = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         'id' => 'integer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      |  Relationships | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Relationship with posts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return \Illuminate\Database\Eloquent\Relations\HasMany | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function posts() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $this->hasMany(Post::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      |  Getters & Setters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Set the name attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param  string  $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function setNameAttribute($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $this->attributes['name'] = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Set the slug attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param  string  $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function setSlugAttribute($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return $this->attributes['slug'] = Str::slug($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Get the translatable attributes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function getTranslatableAttributes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return ['name', 'slug']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      |  Main Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * Create a new category. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param  array  $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @return self | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |     public static function createOne(array $attributes) | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |         $category = (new self) | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |             ->setTranslations('name', $attributes['name']) | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |             ->setTranslations('slug', $attributes['name']); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         $category->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         return $category; | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * Update the current category. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param  array  $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     public function updateOne(array $attributes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->setTranslations('name', $attributes['name']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |              ->setTranslations('slug', $attributes['name']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |              ->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * Get the categories options for select input. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param  bool  $placeholder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @return \Illuminate\Database\Eloquent\Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public static function getSelectOptions($placeholder = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         /** @var  \Illuminate\Database\Eloquent\Collection  $categories */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         $categories = Cache::remember('blog_categories_select_options', 5, function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             return self::all()->keyBy('id')->transform(function (Category $category) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                 return implode(' / ', $category->getTranslations('name')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         return $placeholder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             ? $categories->prepend(trans('blog::categories.select-category'), 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             : $categories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      |  Check Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * Check if category has posts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     public function hasPosts() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         return ! $this->posts->isEmpty(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * Check if the category is deletable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     public function isDeletable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         return ! $this->hasPosts(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |      |  Other Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      * Clear the cached categories. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |     public static function clearCache() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         cache()->forget('blog_categories_select_options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 219 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 220 |  |  |  |