Skip to content

Commit 62b2f94

Browse files
committed
Fix firstWhere behavior for relations
1 parent b815dc6 commit 62b2f94

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,20 @@ public function findOrFail($id, $columns = ['*'])
574574
throw (new ModelNotFoundException)->setModel(get_class($this->related), $id);
575575
}
576576

577+
/**
578+
* Add a basic where clause to the query, and return the first result.
579+
*
580+
* @param \Closure|string|array $column
581+
* @param mixed $operator
582+
* @param mixed $value
583+
* @param string $boolean
584+
* @return \Illuminate\Database\Eloquent\Model|static
585+
*/
586+
public function firstWhere($column, $operator = null, $value = null, $boolean = 'and')
587+
{
588+
return $this->where($column, $operator, $value, $boolean)->first();
589+
}
590+
577591
/**
578592
* Execute the query and get the first result.
579593
*

src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ public function updateOrCreate(array $attributes, array $values = [])
246246
return $instance;
247247
}
248248

249+
/**
250+
* Add a basic where clause to the query, and return the first result.
251+
*
252+
* @param \Closure|string|array $column
253+
* @param mixed $operator
254+
* @param mixed $value
255+
* @param string $boolean
256+
* @return \Illuminate\Database\Eloquent\Model|static
257+
*/
258+
public function firstWhere($column, $operator = null, $value = null, $boolean = 'and')
259+
{
260+
return $this->where($column, $operator, $value, $boolean)->first();
261+
}
262+
249263
/**
250264
* Execute the query and get the first related model.
251265
*

tests/Integration/Database/EloquentBelongsToManyTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,22 @@ public function testWherePivotOnString()
629629
$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());
630630
}
631631

632+
public function testFirstWhere()
633+
{
634+
$tag = Tag::create(['name' => 'foo']);
635+
$post = Post::create(['title' => Str::random()]);
636+
637+
DB::table('posts_tags')->insert([
638+
['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'foo'],
639+
]);
640+
641+
$relationTag = $post->tags()->firstWhere('name', 'foo');
642+
$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());
643+
644+
$relationTag = $post->tags()->firstWhere('name', '=', 'foo');
645+
$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());
646+
}
647+
632648
public function testWherePivotOnBoolean()
633649
{
634650
$tag = Tag::create(['name' => Str::random()]);

tests/Integration/Database/EloquentHasManyThroughTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,27 @@ public function testBasicCreateAndRetrieve()
4848
$user = User::create(['name' => Str::random()]);
4949

5050
$team1 = Team::create(['id' => 10, 'owner_id' => $user->id]);
51-
$team2 = Team::create(['owner_id' => $user->id]);
51+
$team2 = Team::create(['owner_id' => $user->id, 'owner_slug' => 'foo']);
5252

53-
$mate1 = User::create(['name' => Str::random(), 'team_id' => $team1->id]);
54-
$mate2 = User::create(['name' => Str::random(), 'team_id' => $team2->id]);
53+
$mate1 = User::create(['name' => 'John', 'team_id' => $team1->id]);
54+
$mate2 = User::create(['name' => 'Jack', 'team_id' => $team2->id, 'slug' => null]);
5555

5656
User::create(['name' => Str::random()]);
5757

5858
$this->assertEquals([$mate1->id, $mate2->id], $user->teamMates->pluck('id')->toArray());
5959
$this->assertEquals([$user->id], User::has('teamMates')->pluck('id')->toArray());
60+
61+
$result = $user->teamMates()->first();
62+
$this->assertEquals(
63+
$mate1->refresh()->getAttributes() + ['laravel_through_key' => '1'],
64+
$result->getAttributes()
65+
);
66+
67+
$result = $user->teamMates()->firstWhere('name', 'Jack');
68+
$this->assertEquals(
69+
$mate2->refresh()->getAttributes() + ['laravel_through_key' => '1'],
70+
$result->getAttributes()
71+
);
6072
}
6173

6274
public function testGlobalScopeColumns()

0 commit comments

Comments
 (0)