From fca265f2b2c30809704c22b34edd7139418c6d73 Mon Sep 17 00:00:00 2001 From: Magnus Hauge Bakke Date: Wed, 28 Feb 2024 11:14:11 +0100 Subject: [PATCH 1/2] [10.x] Adds documentation for `joinLateral` and `leftJoinLateral` --- queries.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/queries.md b/queries.md index ce92e35c60a..2117ea7fd8b 100644 --- a/queries.md +++ b/queries.md @@ -380,6 +380,26 @@ You may use the `joinSub`, `leftJoinSub`, and `rightJoinSub` methods to join a q $join->on('users.id', '=', 'latest_posts.user_id'); })->get(); + +#### Lateral Joins + +> [!WARNING] +> Lateral joins are currently supported by PostgreSQL, MySQL >= 8.0.14 and SQL Server. + +You may use the `joinLateral` and `leftJoinLateral` methods to perform a "lateral join" with a subquery. Each of these methods receives two arguments: the subquery and its table alias. The join condition(s) should be specified within the `where` clause of this subquery. Lateral joins are evaluated for each row and can reference columns outside the subquery. + +In this example, we will retrieve a collection of users with the users 3 most recent blog posts. Each user can produce up to 3 rows in the result set, one for each of their most recent blog posts. The join condition is specified with a `whereColumn` clause within the subquery, referencing the current user row: + + $latestPosts = DB::table('posts') + ->select('id as post_id', 'title as post_title', 'created_at as post_created_at') + ->whereColumn('user_id', 'users.id') + ->orderBy('created_at', 'desc') + ->limit(3); + + $users = DB::table('users') + ->joinLateral($latestPosts, 'latest_posts') + ->get(); + ## Unions From 694931f182da58053b90557e335fcca7439674f6 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 28 Feb 2024 08:26:29 -0800 Subject: [PATCH 2/2] formatting --- queries.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/queries.md b/queries.md index 2117ea7fd8b..bab2ed2269d 100644 --- a/queries.md +++ b/queries.md @@ -384,11 +384,11 @@ You may use the `joinSub`, `leftJoinSub`, and `rightJoinSub` methods to join a q #### Lateral Joins > [!WARNING] -> Lateral joins are currently supported by PostgreSQL, MySQL >= 8.0.14 and SQL Server. +> Lateral joins are currently supported by PostgreSQL, MySQL >= 8.0.14, and SQL Server. -You may use the `joinLateral` and `leftJoinLateral` methods to perform a "lateral join" with a subquery. Each of these methods receives two arguments: the subquery and its table alias. The join condition(s) should be specified within the `where` clause of this subquery. Lateral joins are evaluated for each row and can reference columns outside the subquery. +You may use the `joinLateral` and `leftJoinLateral` methods to perform a "lateral join" with a subquery. Each of these methods receives two arguments: the subquery and its table alias. The join condition(s) should be specified within the `where` clause of the given subquery. Lateral joins are evaluated for each row and can reference columns outside the subquery. -In this example, we will retrieve a collection of users with the users 3 most recent blog posts. Each user can produce up to 3 rows in the result set, one for each of their most recent blog posts. The join condition is specified with a `whereColumn` clause within the subquery, referencing the current user row: +In this example, we will retrieve a collection of users as well as the user's three most recent blog posts. Each user can produce up to three rows in the result set: one for each of their most recent blog posts. The join condition is specified with a `whereColumn` clause within the subquery, referencing the current user row: $latestPosts = DB::table('posts') ->select('id as post_id', 'title as post_title', 'created_at as post_created_at') @@ -397,8 +397,8 @@ In this example, we will retrieve a collection of users with the users 3 most re ->limit(3); $users = DB::table('users') - ->joinLateral($latestPosts, 'latest_posts') - ->get(); + ->joinLateral($latestPosts, 'latest_posts') + ->get(); ## Unions