Menu.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. public function parent()
  24. {
  25. return $this->belongsTo(Menu::class, 'parent_id', 'id');
  26. }
  27. public function getParentNameAttribute()
  28. {
  29. return Menu::query()->where('id', $this->attributes['parent_id'])->value('name') ?? '--';
  30. }
  31. public function getMetaAttribute()
  32. {
  33. $meta = js2php($this->attributes['meta']);
  34. if (App::getLocale() == 'en') {
  35. $meta['title'] = $this->attributes['zh_title'];
  36. } else {
  37. $meta['title'] = $this->attributes['en_title'];
  38. }
  39. return $meta;
  40. }
  41. }