Menu.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Models\Model;
  4. use Illuminate\Support\Facades\App;
  5. use Prettus\Repository\Contracts\Transformable;
  6. use Prettus\Repository\Traits\TransformableTrait;
  7. /**
  8. * Class Menu.
  9. *
  10. * @package namespace App\Repositories\Models\Base;
  11. */
  12. class Menu extends Model implements Transformable
  13. {
  14. use TransformableTrait;
  15. protected $table = 'base_menus';
  16. protected $casts = ['meta' => 'array'];
  17. /**
  18. * The attributes that are mass assignable.
  19. *
  20. * @var array
  21. */
  22. protected $guarded = [];
  23. // protected static function booted()
  24. // {
  25. // self::language();
  26. // }
  27. public function parent()
  28. {
  29. return $this->belongsTo(Menu::class, 'parent_id', 'id');
  30. }
  31. public function getParentNameAttribute()
  32. {
  33. return Menu::query()->where('id', $this->attributes['parent_id'])->value('name') ?? '--';
  34. }
  35. public function getMetaAttribute()
  36. {
  37. $meta = js2php($this->attributes['meta']);
  38. if (App::getLocale() == 'en') {
  39. $meta['title'] = $this->attributes['zh_title'];
  40. } else {
  41. $meta['title'] = $this->attributes['en_title'];
  42. }
  43. return $meta;
  44. }
  45. }