DistProductCategory.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. 'seo_title', 'seo_keywords', 'seo_description',
  31. ];
  32. protected $distributor = null;
  33. public function __construct()
  34. {
  35. // // 获取当前登录的分销商/经销商
  36. // $distributor=Session::get('distributor');
  37. //
  38. //
  39. //
  40. // // 过滤分销商
  41. // if ($distributor) {
  42. //
  43. // $this->dist_id =$distributor->id;
  44. // }
  45. //
  46. }
  47. protected static function boot()
  48. {
  49. var_dump('ufoxguo');
  50. parent::boot();
  51. static::addGlobalScope(new VerifiedDistId());
  52. }
  53. // protected static function booted()
  54. // {
  55. //
  56. // static::addGlobalScope('dist_id', function (Builder $builder) {
  57. // // 获取当前登录的分销商/经销商
  58. // $distributor=Session::get('distributor');
  59. //
  60. // // 过滤分销商
  61. // if (!$distributor) {
  62. // $builder->where('dist_id', $distributor->id); // 替换为你的条件
  63. // }
  64. // });
  65. //
  66. // }
  67. /*
  68. * 关联产品参数
  69. */
  70. public function distProductParameter()
  71. {
  72. return $this->hasOne(DistProductParameter::class,'id','parameter_id');
  73. }
  74. public static function selectOptions(\Closure $closure = null)
  75. {
  76. $options = (new static())->withQuery($closure)->buildSelectOptions();
  77. return collect($options)->all();
  78. }
  79. public function getAllCategories($parentId = null)
  80. {
  81. // Retrieve categories with the specified parentId, or all if no parentId is specified
  82. $query = self::query();
  83. if ($parentId !== null) {
  84. $query->where('parent_id', $parentId);
  85. }
  86. $categories = $query->orderBy($this->orderColumn)->get();
  87. // If you want a hierarchical structure, you can recursively build it
  88. $categories->each(function ($category) {
  89. $category->children = $category->getAllCategories($category->id);
  90. });
  91. return $categories;
  92. }
  93. }