RouteModel.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Game;
  3. use App\Game\Services\RouteService;
  4. use Illuminate\Database\Eloquent\Model;
  5. class RouteModel extends Model
  6. {
  7. protected $connection='mysql';
  8. const TABLE = 'webgame.routes';
  9. protected $table = self::TABLE;
  10. protected $fillable = [
  11. 'path', 'type', 'side', 'block', 'title', 'icon',
  12. 'fill', 'component', 'query', 'login', 'lpath', 'subs'
  13. ];
  14. // 由于subs是嵌套的,可能需要特别处理或使用访问器
  15. protected $casts = [
  16. 'subs' => 'array'
  17. ];
  18. // 子路由关系
  19. public function subs()
  20. {
  21. return $this->hasMany(RouteModel::class, 'parent_id', 'id')->whereRaw(RouteService::getStateToWhereRaw())->orderBy('index');
  22. }
  23. // 父路由关系,可选,如果你需要从子路由访问父路由
  24. public function parent()
  25. {
  26. return $this->belongsTo(RouteModel::class, 'parent_id', 'id');
  27. }
  28. public function getSubsAttribute($value)
  29. {
  30. return array_map(function ($sub) {
  31. return new self($sub);
  32. }, json_decode($value, true));
  33. }
  34. public function setSubsAttribute($value)
  35. {
  36. $this->attributes['subs'] = json_encode($value);
  37. }
  38. }