File tree Expand file tree Collapse file tree 3 files changed +52
-2
lines changed
Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change 99Homestead.json
1010Homestead.yaml
1111npm-debug.log
12- yarn-error.log
12+ yarn-error.log
13+ SCRATCH.md
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+ ```
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ public function boot()
1818 return $ this ->where ('id ' , function ($ sub ) use ($ where ) {
1919 $ sub ->from ($ this ->from )
2020 ->selectRaw ('max(id) ' )
21- ->whereRaw ($ where ['first ' ] . ' = ' . $ where ['second ' ]);
21+ ->whereColumn ($ where ['first ' ], $ where ['second ' ]);
2222 });
2323 });
2424
You can’t perform that action at this time.
0 commit comments