Skip to content

Commit ea3d9f5

Browse files
committed
added tip #349
1 parent 60aa549 commit ea3d9f5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Daily Laravel/PHP tips I share on my [X](https://x.com/OussamaMater) and [Linked
99

1010
Currently, there are over 300 tips categorized as follows:
1111

12-
- 🗄️ [Eloquent & Database Tips](./tips/eloquent-and-database.md) (97 tips)
12+
- 🗄️ [Eloquent & Database Tips](./tips/eloquent-and-database.md) (98 tips)
1313
- 🛠️ [Helpers Tips](./tips/helpers.md) (65 tips)
1414
- 🧪 [Testing Tips](./tips/testing.md) (29 tips)
1515
- 💻 [Artisan & Console Command Tips](./tips/console.md) (26 tips)

tips/eloquent-and-database.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
- [Restore Trashed Models](#laravel-tip--restore-trashed-models-️)
9898
- [Bootable Traits](#laravel-tip--bootable-traits-️)
9999
- [Boot Traits with Attributes](#laravel-tip--boot-traits-with-attributes-️)
100+
- [The New "whereAttachedTo" Method](#laravel-tip--the-new-whereattachedto-method-️)
100101

101102
## Laravel Tip 💡: Get Original Attributes ([⬆️](#eloquent--database-tips-cd-))
102103

@@ -1981,3 +1982,21 @@ trait HasToken
19811982
}
19821983
}
19831984
```
1985+
1986+
## Laravel Tip 💡: The New "whereAttachedTo" Method ([⬆️](#eloquent--database-tips-cd-))
1987+
1988+
We have all used "whereHas", and sometimes you need to constrain it to match specific models. Since Laravel v12.13, you can use "whereAttachedTo" for exactly that 🚀
1989+
1990+
```php
1991+
<?php
1992+
1993+
$tags = Tag::where('created_at', '>', now()->subMonth())->get();
1994+
1995+
// instead of this 🥱
1996+
Post::whereHas('tags', function (Builder $query) use ($tags) {
1997+
$query->whereKey($tags);
1998+
})->get();
1999+
2000+
// You can do this 🔥
2001+
Post::whereAttachedTo($tags)->get();
2002+
```

0 commit comments

Comments
 (0)