@@ -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);
}
@@ -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) : [],
];
@@ -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');
+ }