Skip to content

Commit

Permalink
Fix firstWhere behavior for relations
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Apr 24, 2020
1 parent b815dc6 commit 62b2f94
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
Expand Down
18 changes: 15 additions & 3 deletions tests/Integration/Database/EloquentHasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,27 @@ public function testBasicCreateAndRetrieve()
$user = User::create(['name' => Str::random()]);

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

$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()
Expand Down

0 comments on commit 62b2f94

Please sign in to comment.