bianjunhui 3 days ago
parent
commit
ca2411f5db

+ 2 - 0
packages/Longyi/FrontMenu/README.md

@@ -58,6 +58,8 @@ php artisan cache:clear
 php artisan config:clear
 php artisan view:clear
 php artisan route:clear
+php artisan bagisto-api:clear-cache
+
 ```
 
 ### 5. 授权角色

+ 10 - 62
packages/Webkul/BagistoApi/src/Models/FrontMenuItem.php

@@ -48,75 +48,23 @@ class FrontMenuItem extends BaseFrontMenuItem
     }
 
     /**
-     * Menu name.
+     * Override the parent relationship to target the API resource model,
+     * so API Platform can resolve the self-referencing relation.
      */
-    #[ApiProperty(readable: true, writable: false)]
-    public function getName(): ?string
+    public function parent()
     {
-        return $this->name;
+        return $this->belongsTo(self::class, 'parent_id');
     }
 
     /**
-     * Unique slug.
+     * Override the children relationship to target the API resource model,
+     * so API Platform can resolve the self-referencing relation.
      */
-    #[ApiProperty(readable: true, writable: false)]
-    public function getSlug(): ?string
+    public function children()
     {
-        return $this->slug;
-    }
-
-    /**
-     * Menu URL.
-     */
-    #[ApiProperty(readable: true, writable: false)]
-    public function getUrl(): ?string
-    {
-        return $this->url;
-    }
-
-    /**
-     * Menu image.
-     */
-    #[ApiProperty(readable: true, writable: false)]
-    public function getImage(): ?string
-    {
-        return $this->image;
-    }
-
-    /**
-     * Menu banner.
-     */
-    #[ApiProperty(readable: true, writable: false)]
-    public function getBanner(): ?string
-    {
-        return $this->banner;
-    }
-
-    /**
-     * Parent menu id.
-     */
-    #[ApiProperty(readable: true, writable: false)]
-    public function getParentId(): ?int
-    {
-        return $this->parent_id ? (int) $this->parent_id : null;
-    }
-
-    /**
-     * Sort order.
-     */
-    #[ApiProperty(readable: true, writable: false)]
-    public function getSortOrder(): int
-    {
-        return (int) $this->sort_order;
-    }
-
-    /**
-     * Status.
-     */
-    #[ApiProperty(readable: true, writable: false)]
-    public function getStatus(): bool
-    {
-        return (bool) $this->status;
+        return $this->hasMany(self::class, 'parent_id')
+            ->orderBy('sort_order')
+            ->orderBy('id');
     }
 
     /**

+ 1 - 3
packages/Webkul/BagistoApi/src/Resolver/FrontMenuTreeResolver.php

@@ -34,9 +34,7 @@ class FrontMenuTreeResolver implements QueryCollectionResolverInterface
             if ($itemParentId === $parentId) {
                 $children = $this->buildTree($items, (int) $item->id);
 
-                if (! empty($children)) {
-                    $item->setRelation('children', collect($children));
-                }
+                $item->setRelation('children', collect($children));
 
                 $tree[] = $item;
             }