'datetime:Y-m-d H:i:s', 'updated_at' => 'datetime:Y-m-d H:i:s', ]; protected $fillable = [ 'name', 'parent_id', 'order','enabled','dist_id','slug','created_at', 'updated_at',// 假设已有的可填充字段 'seo_title', 'seo_keywords', 'seo_description', ]; protected $distributor = null; /* * 关联产品参数 */ public function distProductParameter() { return $this->hasOne(DistProductParameter::class,'id','parameter_id'); } public static function selectOptions(\Closure $closure = null) { $options = (new static())->withQuery($closure)->buildSelectOptions(); return collect($options)->all(); } public function getAllCategories($parentId = null) { // Retrieve categories with the specified parentId, or all if no parentId is specified $query = self::query(); if ($parentId !== null) { $query->where('parent_id', $parentId); } $categories = $query->orderBy($this->orderColumn)->get(); // If you want a hierarchical structure, you can recursively build it $categories->each(function ($category) { $category->children = $category->getAllCategories($category->id); }); return $categories; } }