Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Fix firstWhere behavior for relations #32525

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 14 additions & 2 deletions tests/Integration/Database/EloquentHasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down