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
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.
251
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
-
322
252
### 2 - Querying relationship existence (Using Joins)
323
253
324
254
[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