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

kero's avatar
Level 1

laravel Join Vs With (in Performance)

suppose i have User and Post each User HasMany Post

which of the following code is better

 $result1 =  User::with("posts")
            ->paginate(10);

  $result2 = User::query()
            ->join('posts','users.id','=','posts.user_id')
            ->paginate(10);
0 likes
2 replies
tykus's avatar

That depends on what you want to do with the data - one the benefits of eager-loading (albeit you have two database queries), you get User model instances each with a Collection of Post instances; you do not get this in the second case

1 like
kero's avatar
Level 1

what about in case of big data (millions of records in the database ) which you preferer with or join ??

don't forget there is pagination

1 like

Please or to participate in this conversation.