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
Copy file name to clipboardExpand all lines: README.md
+78-4Lines changed: 78 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ Joins are very useful in a lot of ways. If you are here, you most likely know ab
12
12
13
13
A few things we consider is missing when using joins which are very powerful Eloquent features:
14
14
15
-
* Ability to use relationship definitions to make joins;
15
+
* Ability to use relationship definitions to make joins (inner, left, right, and cross joins);
16
16
* Ability to use model scopes inside different contexts;
17
17
* Ability to query relationship existence using joins instead of where exists;
18
18
* Ability to easily sort results based on columns or aggregations from related tables;
@@ -59,11 +59,12 @@ But, **it gets better** when you need to **join nested relationships**. Let's as
59
59
User::joinRelationship('posts.comments');
60
60
```
61
61
62
-
So much better, wouldn't you agree?! You can also `left`or `right` join the relationships as needed.
62
+
So much better, wouldn't you agree?! You can also `left`, `right`, or `cross` join the relationships as needed.
63
63
64
64
```php
65
65
User::leftJoinRelationship('posts.comments');
66
66
User::rightJoinRelationship('posts.comments');
67
+
User::crossJoinRelationship('posts.comments');
67
68
```
68
69
69
70
#### Joining polymorphic relationships
@@ -145,10 +146,11 @@ When using model scopes inside a join clause, you **can't** type hint the `$quer
145
146
146
147
#### Using aliases
147
148
148
-
Sometimes, you are going to need to use table aliases on your joins because you are joining the same table more than once. One option to accomplish this is to use the `joinRelationshipUsingAlias` method.
149
+
Sometimes, you are going to need to use table aliases on your joins because you are joining the same table more than once. One option to accomplish this is to use the `joinRelationshipUsingAlias` method. This works for all join types including cross joins.
There's, though, a gotcha here. Your global scope **cannot** type-hint the `Eloquent\Builder` class in the first parameter of the `apply` method, otherwise you will get errors.
247
251
252
+
#### Cross Joins
253
+
254
+
Cross joins generate a cartesian product between the first table and the joined table. This package provides cross join support for relationships, following the same patterns as other join types.
255
+
256
+
```php
257
+
// Basic cross join
258
+
User::crossJoinRelationship('posts');
259
+
260
+
// Cross join with nested relationships
261
+
User::crossJoinRelationship('posts.comments');
262
+
263
+
// Cross join with aliases
264
+
User::crossJoinRelationshipUsingAlias('posts');
265
+
```
266
+
267
+
**Cross joins with conditions and scopes**
268
+
269
+
Even though cross joins don't typically have join conditions, you can still apply conditions and use model scopes in the callback. These conditions will be applied to the main query's WHERE clause:
270
+
271
+
```php
272
+
// Using model scopes
273
+
User::crossJoinRelationship('posts', function ($join) {
274
+
$join->published();
275
+
});
276
+
277
+
// Using custom conditions
278
+
User::crossJoinRelationship('posts', function ($join) {
User::crossJoinRelationship('posts', function ($join) {
284
+
$join->as('p');
285
+
});
286
+
```
287
+
288
+
**Cross joins with soft deletes**
289
+
290
+
Cross joins automatically handle soft deletes, just like other join types:
291
+
292
+
```php
293
+
// Excludes soft deleted posts (default behavior)
294
+
User::crossJoinRelationship('posts');
295
+
296
+
// Includes soft deleted posts
297
+
User::crossJoinRelationship('posts', function ($join) {
298
+
$join->withTrashed();
299
+
});
300
+
```
301
+
302
+
**Cross joins with BelongsToMany relationships**
303
+
304
+
Cross joins work with all relationship types, including many-to-many relationships:
305
+
306
+
```php
307
+
// Cross join posts with their tags (through pivot table)
308
+
Post::crossJoinRelationship('tags');
309
+
```
310
+
311
+
**Understanding cartesian products**
312
+
313
+
Cross joins create a cartesian product, meaning every row from the first table is combined with every row from the second table:
314
+
315
+
```php
316
+
// If you have 2 users and 3 posts, this will return 6 rows (2 × 3)
317
+
$results = User::crossJoinRelationship('posts')
318
+
->select('users.name', 'posts.title')
319
+
->get();
320
+
```
321
+
248
322
### 2 - Querying relationship existence (Using Joins)
249
323
250
324
[Querying relationship existence](https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence) is a very powerful and convenient feature of Eloquent. However, it uses the `where exists` syntax which is not always the best and may not be the more performant choice, depending on how many records you have or the structure of your tables.
0 commit comments