Skip to content

[9.x] Model::whereRelation ignores the callback function or does it wrong #42465

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

Closed
wants to merge 4 commits into from
Closed
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
12 changes: 10 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ public function orWhereDoesntHaveMorph($relation, $types, Closure $callback = nu
public function whereRelation($relation, $column, $operator = null, $value = null)
{
return $this->whereHas($relation, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
if ($column instanceof Closure) {
$column($query);
} else {
$query->where($column, $operator, $value);
}
});
}

Expand All @@ -381,7 +385,11 @@ public function whereRelation($relation, $column, $operator = null, $value = nul
public function orWhereRelation($relation, $column, $operator = null, $value = null)
{
return $this->orWhereHas($relation, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
if ($column instanceof Closure) {
$column($query);
} else {
$query->where($column, $operator, $value);
}
});
}

Expand Down
71 changes: 71 additions & 0 deletions tests/Integration/Database/EloquentWhereHasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Illuminate\Tests\Integration\Database\EloquentWhereHasTest;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

Expand Down Expand Up @@ -44,6 +47,74 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
(new Text(['content' => 'test2']))->post()->associate($post)->save();
}

/**
* Check that the 'whereRelation' callback function works.
*
* @dataProvider dataProviderWhereRelationCallback
*/
public function testWhereRelationCallback($callbackEloquent, $callbackQuery)
{
$userWhereRelation = User::whereRelation('posts', $callbackEloquent);
$userWhereHas = User::whereHas('posts', $callbackEloquent);
$query = DB::table('users')->whereExists($callbackQuery);

$this->assertEquals($userWhereRelation->getQuery()->toSql(), $query->toSql());
$this->assertEquals($userWhereRelation->getQuery()->toSql(), $userWhereHas->toSql());
$this->assertEquals($userWhereHas->getQuery()->toSql(), $query->toSql());

$this->assertEquals($userWhereRelation->first()->id, $query->first()->id);
$this->assertEquals($userWhereRelation->first()->id, $userWhereHas->first()->id);
$this->assertEquals($userWhereHas->first()->id, $query->first()->id);
}

/**
* Check that the 'orWhereRelation' callback function works.
*
* @dataProvider dataProviderWhereRelationCallback
*/
public function testOrWhereRelationCallback($callbackEloquent, $callbackQuery)
{
$userOrWhereRelation = User::orWhereRelation('posts', $callbackEloquent);
$userOrWhereHas = User::orWhereHas('posts', $callbackEloquent);
$query = DB::table('users')->orWhereExists($callbackQuery);

$this->assertEquals($userOrWhereRelation->getQuery()->toSql(), $query->toSql());
$this->assertEquals($userOrWhereRelation->getQuery()->toSql(), $userOrWhereHas->toSql());
$this->assertEquals($userOrWhereHas->getQuery()->toSql(), $query->toSql());

$this->assertEquals($userOrWhereRelation->first()->id, $query->first()->id);
$this->assertEquals($userOrWhereRelation->first()->id, $userOrWhereHas->first()->id);
$this->assertEquals($userOrWhereHas->first()->id, $query->first()->id);
}

public function dataProviderWhereRelationCallback()
{
$callbackArray = function ($value) {
$callbackEloquent = function (EloquentBuilder $builder) use ($value) {
$builder->selectRaw('id')->where('public', $value);
};

$callbackQuery = function (QueryBuilder $builder) use ($value) {
$hasMany = app()->make(User::class)->posts();

$builder->from('posts')->addSelect(['*'])->whereColumn(
$hasMany->getQualifiedParentKeyName(),
'=',
$hasMany->getQualifiedForeignKeyName()
);

$builder->selectRaw('id')->where('public', $value);
};

return [$callbackEloquent, $callbackQuery];
};

return [
'Find user with post.public = true' => $callbackArray(true),
'Find user with post.public = false' => $callbackArray(false),
];
}

public function testWhereRelation()
{
$users = User::whereRelation('posts', 'public', true)->get();
Expand Down