Browse Source

load children plans

moell 1 year ago
parent
commit
cb506a7b35

+ 1 - 1
app/Http/Controllers/API/PlanController.php

@@ -16,7 +16,7 @@ class PlanController extends Controller
      */
     public function index(Request $request)
     {
-        $plans = Plan::filter($request->all())->simplePaginate();
+        $plans = Plan::filter($request->all())->where("parent_id", 0)->with(['children'])->simplePaginate();
 
         return PlanResource::collection($plans);
     }

+ 1 - 0
app/Http/Resources/API/PlanResource.php

@@ -24,6 +24,7 @@ class PlanResource extends JsonResource
             'begin' => $this->begin,
             'end' => $this->end,
             'description' => $this->description,
+            'children' => $this->parent_id == 0 ? PlanResource::collection($this->children) : [],
         ];
     }
 }

+ 5 - 0
app/Models/Plan.php

@@ -38,4 +38,9 @@ class Plan extends Model
     {
         return $this->belongsToMany(Project::class, 'project_plan', 'plan_id', 'project_id');
     }
+
+    public function children()
+    {
+        return $this->hasMany(Plan::class, 'parent_id');
+    }
 }