Join
Laravel Eloquent ORM Join
Left Join
$user_table = (new UserModel)->getTable();
$comment_table = (new CommentModel)->getTable();
$comment_status = 'enable';
$user_status = 'enable';
$UserCommentCollection = UserModel::select(['*'])
->leftJoin($comment_table, function($JoinSubQuery) use (
$comment_table,
$user_table,
$comment_status,
) {
$JoinSubQuery->on("{$user_table}.id", "{$comment_table}.user_id")
->where("{$comment_table}.status", $comment_status);
})
->where("{$user_table}.status", $user_status);
->skip(10)
->take(100)
->get();
SELECT
*
FROM
"user"
LEFT JOIN "comment"
ON "user"."id" = "comment"."user_id"
AND "comment"."status" = 'enable'
WHERE
"user"."status" = 'enable'
LIMIT 100 OFFSET 10