You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know we already have Subquery Joins, but there's no way (except raw query) to use subquery in the $second parameter for joins
Currently we can achieve it like this:
$builder = DB::table('authors as a')
->select('a.name', 'next_book.name as nextBookName')
->join(
'books as next_book',
'next_book.id',
'=',
DB::raw('(SELECT id FROM books WHERE author_id = a.id AND released_at > NOW() ORDER BY released_at ASC LIMIT 1)')
);
But it would be nice to use it like this:
$subQuery = DB::table('books')
->where('author_id', DB::raw('a.id'))
->where('released_at', '>', DB::raw('NOW()'))
->orderBy('released_at')
->limit(1);
$builder = DB::table('authors as a')
->select('a.name', 'next_book.name as nextBookName')
->join('books as next_book', 'next_book.id', '=', $subQuery);
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I know we already have Subquery Joins, but there's no way (except raw query) to use subquery in the
$second
parameter for joinsCurrently we can achieve it like this:
But it would be nice to use it like this:
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions