Answer the question
In order to leave comments, you need to log in
Laravel Eager Loading vs Joins?
//eager
$items = UserReport::with(['owner' => function ($query) {
$query->where('nickname', 'LIKE', '%Test%');
}, 'reported'])->get();
// 0.14 s
// 4 queries
// joins
$items = UserReport::where('owner.nickname', 'LIKE', '%Test%')
->select('owner.nickname as owner_nickname', 'reported.nickname as reported_nickname',
'user_report_reasons.reason', 'user_reports.read' )
->leftJoin('users as owner', 'user_reports.from_user_id', '=', 'owner.id')
->leftJoin('users as reported', 'user_reports.reported_user_id', '=', 'reported.id')
->join('user_report_reasons', 'user_reports.reason_id', '=', 'user_report_reasons.id')
->get();
// 0.23 s
// 2 queries
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question