DistProductCategory.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Models;
  3. use Dcat\Admin\Traits\HasDateTimeFormatter;
  4. use Dcat\Admin\Traits\ModelTree;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\Session;
  7. use Spatie\EloquentSortable\Sortable;
  8. use Illuminate\Database\Eloquent\Scope;
  9. use App\Distributor\Scopes;
  10. class DistProductCategory extends Model implements Sortable
  11. {
  12. use HasDateTimeFormatter,
  13. ModelTree {
  14. ModelTree::boot as treeBoot;
  15. }
  16. use HasDateTimeFormatter;
  17. protected $table = 'dist_product_category';
  18. //名称
  19. protected $titleColumn = 'name';
  20. //排序
  21. protected $orderColumn = 'order';
  22. //父级
  23. protected $parentColumn = 'parent_id';
  24. protected $casts = [
  25. 'created_at' => 'datetime:Y-m-d H:i:s',
  26. 'updated_at' => 'datetime:Y-m-d H:i:s',
  27. ];
  28. protected $fillable = [
  29. 'name', 'parent_id', 'order','enabled','dist_id','slug','created_at', 'updated_at',// 假设已有的可填充字段
  30. ];
  31. protected $distributor = null;
  32. public function __construct()
  33. {
  34. // // 获取当前登录的分销商/经销商
  35. // $distributor=Session::get('distributor');
  36. //
  37. //
  38. //
  39. // // 过滤分销商
  40. // if ($distributor) {
  41. //
  42. // $this->dist_id =$distributor->id;
  43. // }
  44. //
  45. }
  46. protected static function boot()
  47. {
  48. var_dump('ufoxguo');
  49. parent::boot();
  50. static::addGlobalScope(new VerifiedDistId());
  51. }
  52. // protected static function booted()
  53. // {
  54. //
  55. // static::addGlobalScope('dist_id', function (Builder $builder) {
  56. // // 获取当前登录的分销商/经销商
  57. // $distributor=Session::get('distributor');
  58. //
  59. // // 过滤分销商
  60. // if (!$distributor) {
  61. // $builder->where('dist_id', $distributor->id); // 替换为你的条件
  62. // }
  63. // });
  64. //
  65. // }
  66. /*
  67. * 关联产品参数
  68. */
  69. public function distProductParameter()
  70. {
  71. return $this->hasOne(DistProductParameter::class,'id','parameter_id');
  72. }
  73. public static function selectOptions(\Closure $closure = null)
  74. {
  75. $options = (new static())->withQuery($closure)->buildSelectOptions();
  76. return collect($options)->all();
  77. }
  78. public function getAllCategories($parentId = null)
  79. {
  80. // Retrieve categories with the specified parentId, or all if no parentId is specified
  81. $query = self::query();
  82. if ($parentId !== null) {
  83. $query->where('parent_id', $parentId);
  84. }
  85. $categories = $query->orderBy($this->orderColumn)->get();
  86. // If you want a hierarchical structure, you can recursively build it
  87. $categories->each(function ($category) {
  88. $category->children = $category->getAllCategories($category->id);
  89. });
  90. return $categories;
  91. }
  92. }