File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ # Laravel Latest Relation
2+ Eloquent macros for querying the latest HasMany relationship in Laravel
3+
4+ ## Installation
5+ Install via composer:
6+ ` composer require nullthoughts/laravel-latest-relation `
7+
8+ ## Usage / Examples
9+ Use the Builder methods inside a whereHas closure:
10+
11+ ### whereLatest($column, $value)
12+ ** Query**
13+ ``` php
14+ $users = User::whereHas('logins', function ($query) {
15+ $query->whereLatest('device_type', 'desktop');
16+ });
17+ ```
18+
19+ ** Dynamic Scope**
20+ ``` php
21+ public function scopeByCondition($query, $condition)
22+ {
23+ return $query->whereHas('logins', function ($query) use ($device) {
24+ $query->whereLatest('device_type', $device);
25+ });
26+ }
27+ ```
28+
29+ ### latestRelation()
30+ ** Query**
31+ ``` php
32+ $users = User::whereHas('logins', function ($query) {
33+ $query->latestRelation()->whereBetween(
34+ 'created_at', [
35+ Carbon::now()->startOfDay(),
36+ Carbon::now()->endOfDay()
37+ ]);
38+ });
39+ ```
40+
41+ ** Dynamic Scope**
42+ ``` php
43+ public function scopeByCondition($query, $condition)
44+ {
45+ return $query->whereHas('logins', function ($query) {
46+ $query->latestRelation()->whereNotNull('device_type');
47+ });
48+ }
49+ ```
You can’t perform that action at this time.
0 commit comments