SystemMediaCategory.php 730 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Models;
  3. use App\Traits\ModelTree;
  4. class SystemMediaCategory extends Model
  5. {
  6. use ModelTree {
  7. delete as modelTreeDelete;
  8. }
  9. protected $fillable = ['parent_id', 'name'];
  10. protected $casts = [
  11. 'parent_id' => 'integer',
  12. ];
  13. public function media()
  14. {
  15. return $this->hasMany(SystemMedia::class, 'category_id');
  16. }
  17. public function setParentIdAttribute($value)
  18. {
  19. $this->attributes['parent_id'] = $value ?: 0;
  20. }
  21. protected function orderColumn()
  22. {
  23. return 'id';
  24. }
  25. public function delete()
  26. {
  27. $res = $this->modelTreeDelete();
  28. $this->media()->update(['category_id' => 0]);
  29. return $res;
  30. }
  31. }