Skip to content

Commit 5df01e3

Browse files
committed
Documentation WIP
1 parent cc61929 commit 5df01e3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
```

0 commit comments

Comments
 (0)