Skip to content

Commit 265e4b7

Browse files
authored
Merge pull request #8 from vicgonvt/master
Adds the operator to the where clauses allowing for single line chaining
2 parents 0865e3f + fa29b16 commit 265e4b7

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

src/ServiceProvider.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function boot()
2222
->whereColumn($where['first'], $where['second']);
2323
});
2424
});
25-
25+
2626
QueryBuilder::macro('earliestRelation', function () {
2727
return $this->relation('min');
2828
});
@@ -31,24 +31,24 @@ public function boot()
3131
return $this->relation('max');
3232
});
3333

34-
QueryBuilder::macro('whereEarliest', function ($column, $value) {
35-
return $this->earliestRelation()->where($column, $value);
34+
QueryBuilder::macro('whereEarliest', function ($column, $operator = null, $value = null) {
35+
return $this->earliestRelation()->where($column, $operator, $value);
3636
});
3737

38-
QueryBuilder::macro('whereLatest', function ($column, $value) {
39-
return $this->latestRelation()->where($column, $value);
38+
QueryBuilder::macro('whereLatest', function ($column, $operator = null, $value = null) {
39+
return $this->latestRelation()->where($column, $operator, $value);
4040
});
4141

42-
EloquentBuilder::macro('whereEarliestRelation', function ($relation, $column, $value) {
43-
return $this->whereHas($relation, function($query) use ($column, $value) {
44-
return $query->whereEarliest($column, $value);
42+
EloquentBuilder::macro('whereEarliestRelation', function ($relation, $column, $operator = null, $value = null) {
43+
return $this->whereHas($relation, function($query) use ($column, $operator, $value) {
44+
return $query->whereEarliest($column, $operator, $value);
4545
});
4646
});
4747

48-
EloquentBuilder::macro('whereLatestRelation', function ($relation, $column, $value) {
49-
return $this->whereHas($relation, function ($query) use ($column, $value) {
50-
$query->whereLatest($column, $value);
48+
EloquentBuilder::macro('whereLatestRelation', function ($relation, $column, $operator = null, $value = null) {
49+
return $this->whereHas($relation, function ($query) use ($column, $operator, $value) {
50+
$query->whereLatest($column, $operator, $value);
5151
});
5252
});
5353
}
54-
}
54+
}

tests/Unit/EloquentLatestRelationTest.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function setUp(): void
5050
'device_type' => 'mobile',
5151
'country' => null
5252
], [
53-
53+
5454
'user_id' => 2,
5555
'created_at' => Carbon::now()->subDays(3),
5656
'device_type' => 'mobile',
@@ -109,9 +109,9 @@ public function latest_relation()
109109
$loggedInYesterday = User::whereHas('logins', function ($query) {
110110
$query->latestRelation()->whereBetween(
111111
'created_at', [
112-
Carbon::now()->subDay(1)->startOfDay(),
113-
Carbon::now()->subDay(1)->endOfDay()
114-
]);
112+
Carbon::now()->subDay(1)->startOfDay(),
113+
Carbon::now()->subDay(1)->endOfDay()
114+
]);
115115
});
116116

117117
$this->assertSame(1, $loggedInYesterday->count());
@@ -121,7 +121,7 @@ public function latest_relation()
121121
/**
122122
* @test
123123
*/
124-
public function earilest_relation()
124+
public function earliest_relation()
125125
{
126126
$users = User::whereHas('logins', function ($query) {
127127
$query->earliestRelation()->whereNotNull('country');
@@ -160,12 +160,12 @@ public function where_latest()
160160
/**
161161
* @test
162162
*/
163-
public function where_earilest()
163+
public function where_earliest()
164164
{
165165
$users = User::whereHas('logins', function ($query) {
166166
$query->whereEarliest('device_type', 'mobile');
167167
})->get();
168-
168+
169169
$this->assertCount(3, $users);
170170

171171
$users = User::whereHas('logins', function ($query) {
@@ -181,7 +181,7 @@ public function where_earilest()
181181
public function where_latest_relation()
182182
{
183183
$users = User::whereLatestRelation('logins', 'device_type', 'mobile')->get();
184-
184+
185185
$this->assertCount(2, $users);
186186
$this->assertSame('Cameron Frye', $users->first()->name);
187187
$this->assertSame('mobile', $users->first()->lastLogin->device_type);
@@ -200,16 +200,38 @@ public function where_latest_relation()
200200
/**
201201
* @test
202202
*/
203-
public function where_earilest_relation()
203+
public function where_earliest_relation()
204204
{
205205
$users = User::whereEarliestRelation('logins', 'device_type', 'mobile')->get();
206-
206+
207207
$this->assertCount(3, $users);
208208

209209
$users = User::whereEarliestRelation('logins', 'device_type', 'desktop')->get();
210210

211211
$this->assertCount(0, $users);
212212
}
213+
214+
/**
215+
* @test
216+
*/
217+
public function where_latest_relation_country_is_not_null()
218+
{
219+
$users = User::whereLatestRelation('logins', 'country', '!=', 'null');
220+
221+
$this->assertSame(1, $users->count());
222+
$this->assertSame('Ferris Bueller', $users->first()->name);
223+
}
224+
225+
/**
226+
* @test
227+
*/
228+
public function where_earliest_relation_country_is_not_null()
229+
{
230+
$users = User::whereEarliestRelation('logins', 'country', '!=', 'null');
231+
232+
$this->assertSame(1, $users->count());
233+
$this->assertSame('Ed Rooney', $users->first()->name);
234+
}
213235
}
214236

215237
class User extends Model
@@ -231,4 +253,4 @@ public function user()
231253
{
232254
return $this->belongsTo(User::class);
233255
}
234-
}
256+
}

0 commit comments

Comments
 (0)