I assume you are only getting 1 level deep and not 2 as in your example?
You can try
MenuItem::where('parent_id',null)->with('children.children')->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I need assistance in improving my query. what I'm doing getting nested self-relation records. here is my migration.
Schema::create('menu_items', function($table) {
$table->increments('id');
$table->string('name');
$table->string('url');
$table->integer('parent_id')->unsigned()->nullable();
$table->foreign('parent_id')->references('id')->on('menu_items');
$table->timestamps();
});
Relation in Model
public function children()
{
return $this->hasMany(MenuItem::class, 'parent_id')->with('children');
}
Query in Controller
MenuItem::where('parent_id',null)->with('children')->get();
The output I need.
[
{
"id": 1,
"name": "All events",
"url": "/events",
"parent_id": null,
"created_at": "2021-04-27T15:35:15.000000Z",
"updated_at": "2021-04-27T15:35:15.000000Z",
"children": [
{
"id": 2,
"name": "Laracon",
"url": "/events/laracon",
"parent_id": 1,
"created_at": "2021-04-27T15:35:15.000000Z",
"updated_at": "2021-04-27T15:35:15.000000Z",
"children": [
{
"id": 3,
"name": "Illuminate your knowledge of the laravel code base",
"url": "/events/laracon/workshops/illuminate",
"parent_id": 2,
"created_at": "2021-04-27T15:35:15.000000Z",
"updated_at": "2021-04-27T15:35:15.000000Z",
"children": []
},
{
"id": 4,
"name": "The new Eloquent - load more with less",
"url": "/events/laracon/workshops/eloquent",
"parent_id": 2,
"created_at": "2021-04-27T15:35:15.000000Z",
"updated_at": "2021-04-27T15:35:15.000000Z",
"children": []
}
]
},
{
"id": 5,
"name": "Reactcon",
"url": "/events/reactcon",
"parent_id": 1,
"created_at": "2021-04-27T15:35:15.000000Z",
"updated_at": "2021-04-27T15:35:15.000000Z",
"children": [
{
"id": 6,
"name": "#NoClass pure functional programming",
"url": "/events/reactcon/workshops/noclass",
"parent_id": 5,
"created_at": "2021-04-27T15:35:15.000000Z",
"updated_at": "2021-04-27T15:35:15.000000Z",
"children": []
},
{
"id": 7,
"name": "Navigating the function jungle",
"url": "/events/reactcon/workshops/jungle",
"parent_id": 5,
"created_at": "2021-04-27T15:35:15.000000Z",
"updated_at": "2021-04-27T15:35:15.000000Z",
"children": []
}
]
}
]
}
]
I am getting what I need with this query. I there any better way to do this if possible in one query?
@rehman_invozone Yeah I even gave you a link to a package that should solve it?
Please or to participate in this conversation.