Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aureliee123's avatar

merge multiple hasMany

Hello , i'm working on a laravel project in which i have three tables module section and data

module hasmany sections and section hasmany datas

is there a way to do something like this merge the two hasmany module::with('sections')->with('datas')->get();

0 likes
2 replies
James_Moore's avatar

You can pass an array of relationships to the with method.

Eager Loading Multiple Relationships

$books = App\Book::with(['author', 'publisher'])->get();

aureliee123's avatar

doesn't work in my case because

the module isn't related to both datas and sections

but

module hasmany sections

section hasmany datas

i get the error Call to undefined relationship [datas] on model [App\module].

Please or to participate in this conversation.