diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index cf8b902b6bea..59490329f341 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -574,6 +574,20 @@ public function findOrFail($id, $columns = ['*']) throw (new ModelNotFoundException)->setModel(get_class($this->related), $id); } + /** + * Add a basic where clause to the query, and return the first result. + * + * @param \Closure|string|array $column + * @param mixed $operator + * @param mixed $value + * @param string $boolean + * @return \Illuminate\Database\Eloquent\Model|static + */ + public function firstWhere($column, $operator = null, $value = null, $boolean = 'and') + { + return $this->where($column, $operator, $value, $boolean)->first(); + } + /** * Execute the query and get the first result. * diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index 1372f628da78..3fe3b8d5de98 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -246,6 +246,20 @@ public function updateOrCreate(array $attributes, array $values = []) return $instance; } + /** + * Add a basic where clause to the query, and return the first result. + * + * @param \Closure|string|array $column + * @param mixed $operator + * @param mixed $value + * @param string $boolean + * @return \Illuminate\Database\Eloquent\Model|static + */ + public function firstWhere($column, $operator = null, $value = null, $boolean = 'and') + { + return $this->where($column, $operator, $value, $boolean)->first(); + } + /** * Execute the query and get the first related model. * diff --git a/tests/Integration/Database/EloquentBelongsToManyTest.php b/tests/Integration/Database/EloquentBelongsToManyTest.php index d3a2ed4f4e5e..32be59a479c5 100644 --- a/tests/Integration/Database/EloquentBelongsToManyTest.php +++ b/tests/Integration/Database/EloquentBelongsToManyTest.php @@ -629,6 +629,22 @@ public function testWherePivotOnString() $this->assertEquals($relationTag->getAttributes(), $tag->getAttributes()); } + public function testFirstWhere() + { + $tag = Tag::create(['name' => 'foo']); + $post = Post::create(['title' => Str::random()]); + + DB::table('posts_tags')->insert([ + ['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'foo'], + ]); + + $relationTag = $post->tags()->firstWhere('name', 'foo'); + $this->assertEquals($relationTag->getAttributes(), $tag->getAttributes()); + + $relationTag = $post->tags()->firstWhere('name', '=', 'foo'); + $this->assertEquals($relationTag->getAttributes(), $tag->getAttributes()); + } + public function testWherePivotOnBoolean() { $tag = Tag::create(['name' => Str::random()]); diff --git a/tests/Integration/Database/EloquentHasManyThroughTest.php b/tests/Integration/Database/EloquentHasManyThroughTest.php index a8fc63822136..08fc7254f95a 100644 --- a/tests/Integration/Database/EloquentHasManyThroughTest.php +++ b/tests/Integration/Database/EloquentHasManyThroughTest.php @@ -50,13 +50,25 @@ public function testBasicCreateAndRetrieve() $team1 = Team::create(['id' => 10, 'owner_id' => $user->id]); $team2 = Team::create(['owner_id' => $user->id]); - $mate1 = User::create(['name' => Str::random(), 'team_id' => $team1->id]); - $mate2 = User::create(['name' => Str::random(), 'team_id' => $team2->id]); + $mate1 = User::create(['name' => 'John', 'team_id' => $team1->id]); + $mate2 = User::create(['name' => 'Jack', 'team_id' => $team2->id, 'slug' => null]); User::create(['name' => Str::random()]); $this->assertEquals([$mate1->id, $mate2->id], $user->teamMates->pluck('id')->toArray()); $this->assertEquals([$user->id], User::has('teamMates')->pluck('id')->toArray()); + + $result = $user->teamMates()->first(); + $this->assertEquals( + $mate1->refresh()->getAttributes() + ['laravel_through_key' => '1'], + $result->getAttributes() + ); + + $result = $user->teamMates()->firstWhere('name', 'Jack'); + $this->assertEquals( + $mate2->refresh()->getAttributes() + ['laravel_through_key' => '1'], + $result->getAttributes() + ); } public function testGlobalScopeColumns()