lundi 27 février 2017

Which is the best choice between Querry builder && Eager loading in laravel 5.4?

I have 3 table: users, posts and comments. I have 2 way for selecting data.

$posts = User::select('id')
            ->with(['post' => function( $query ){
                $query->with(['comments']);
            }])->find($userId);

and

$posts = DB::table('posts')
        ->leftjoin('comments', 'posts.id', '=', 'comments.post_id')
        ->where('user_id', $userId)
        ->get();

I see method 2 is faster than method 1 in some records. I don't know what happen when I have 1000 or 10000 records. Can you tell me which is better? Thanks for any help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire