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

[8.x] Revert "[8.x] Add nested relationships to whereRelation function (#39064)" #39130

Merged
merged 2 commits into from
Oct 7, 2021
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
40 changes: 6 additions & 34 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,23 +362,9 @@ public function orWhereDoesntHaveMorph($relation, $types, Closure $callback = nu
*/
public function whereRelation($relation, $column, $operator = null, $value = null)
{
$relations = collect(explode('.', $relation));

return $this->when(
$relations->count() == 1,
function ($query) use ($relations, $column, $operator, $value) {
$query->whereHas($relations->first(), function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
},
function ($query) use ($relations, $column, $operator, $value) {
$query->whereHas($relations->first(), function ($query) use ($relations, $column, $operator, $value) {
$relations->shift();

$query->whereRelation($relations->implode('.'), $column, $operator, $value);
});
}
);
return $this->whereHas($relation, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
}

/**
Expand All @@ -392,23 +378,9 @@ function ($query) use ($relations, $column, $operator, $value) {
*/
public function orWhereRelation($relation, $column, $operator = null, $value = null)
{
$relations = collect(explode('.', $relation));

return $this->when(
$relations->count() == 1,
function ($query) use ($relations, $column, $operator, $value) {
$query->orWhereHas($relations->first(), function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
},
function ($query) use ($relations, $column, $operator, $value) {
$query->orWhereHas($relations->first(), function ($query) use ($relations, $column, $operator, $value) {
$relations->shift();

$query->orWhereRelation($relations->implode('.'), $column, $operator, $value);
});
}
);
return $this->orWhereHas($relation, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/Integration/Database/EloquentWhereHasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ protected function setUp(): void
$table->boolean('public');
});

Schema::create('texts', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('post_id');
$table->boolean('content');
});

Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('commentable_type');
Expand All @@ -35,10 +41,12 @@ protected function setUp(): void
$user = User::create();
$post = tap((new Post(['public' => true]))->user()->associate($user))->save();
(new Comment)->commentable()->associate($post)->save();
(new Text(['content' => 'test']))->post()->associate($post)->save();

$user = User::create();
$post = tap((new Post(['public' => false]))->user()->associate($user))->save();
(new Comment)->commentable()->associate($post)->save();
(new Text(['content' => 'test2']))->post()->associate($post)->save();
}

public function testWhereRelation()
Expand All @@ -55,6 +63,20 @@ public function testOrWhereRelation()
$this->assertEquals([1, 2], $users->pluck('id')->all());
}

public function testNestedWhereRelation()
{
$texts = User::whereRelation('posts.texts', 'content', 'test')->get();

$this->assertEquals([1], $texts->pluck('id')->all());
}

public function testNestedOrWhereRelation()
{
$texts = User::whereRelation('posts.texts', 'content', 'test')->orWhereRelation('posts.texts', 'content', 'test2')->get();

$this->assertEquals([1, 2], $texts->pluck('id')->all());
}

public function testWhereMorphRelation()
{
$comments = Comment::whereMorphRelation('commentable', '*', 'public', true)->get();
Expand Down Expand Up @@ -104,12 +126,29 @@ public function comments()
return $this->morphMany(Comment::class, 'commentable');
}

public function texts()
{
return $this->hasMany(Text::class);
}

public function user()
{
return $this->belongsTo(User::class);
}
}

class Text extends Model
{
public $timestamps = false;

protected $guarded = [];

public function post()
{
return $this->belongsTo(Post::class);
}
}

class User extends Model
{
public $timestamps = false;
Expand Down