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

[9.x] Add wherehas soft deleting scopes #42100

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -713,16 +713,42 @@ public function mergeConstraintsFrom(Builder $from)
{
$whereBindings = $from->getQuery()->getRawBindings()['where'] ?? [];

$wheres = $from->getQuery()->from !== $this->getQuery()->from
? $this->requalifyWhereTables(
$from->getQuery()->wheres,
$from->getQuery()->from,
$this->getModel()->getTable()
) : $from->getQuery()->wheres;

// Here we have some other query that we want to merge the where constraints from. We will
// copy over any where constraints on the query as well as remove any global scopes the
// query might have removed. Then we will return ourselves with the finished merging.
return $this->withoutGlobalScopes(
$from->removedScopes()
)->mergeWheres(
$from->getQuery()->wheres, $whereBindings
$wheres, $whereBindings
);
}

/**
* Updates the table name for any columns with a new qualified name.
*
* @param array $wheres
* @param string $from
* @param string $to
* @return array
*/
protected function requalifyWhereTables(array $wheres, string $from, string $to): array
{
return collect($wheres)->map(function ($where) use ($from, $to) {
return collect($where)->map(function ($value) use ($from, $to) {
return str_starts_with($value, $from.'.')
? $to.'.'.Str::afterLast($value, '.')
: $value;
});
})->toArray();
}

/**
* Add a sub-query count clause to this query.
*
Expand Down
22 changes: 21 additions & 1 deletion tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function createSchema()
{
$this->schema()->create('users', function ($table) {
$table->increments('id');
$table->integer('user_id')->nullable(); // circular reference to parent User
$table->integer('group_id')->nullable();
$table->string('email')->unique();
$table->timestamps();
Expand Down Expand Up @@ -878,14 +879,28 @@ public function testMorphToNonSoftDeletingModel()
$this->assertNull($comment->owner);
}

public function testSelfReferencingRelationshipWithSoftDeletes()
{
/*
* https://github.com/laravel/framework/issues/42075
*/
[$taylor, $abigail] = $this->createUsers();

$this->assertCount(1, $abigail->self_referencing);
$this->assertTrue($abigail->self_referencing->first()->is($taylor));

$this->assertCount(0, $taylor->self_referencing);
$this->assertEquals(1, SoftDeletesTestUser::whereHas('self_referencing')->count());
}
Copy link
Contributor

@BrandonSurowiec BrandonSurowiec Apr 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would match the code style of the rest of the tests (by using $this over self:: and inline your variables $versionsA and $versionsT. More like this:

    public function testSelfReferencingRelationshipWithSoftDeletes()
    {
        [$taylor, $abigail] = $this->createUsers();

        $this->assertCount(1, $abigail->versions);
        $this->assertTrue($abigail->versions->first()->is($taylor));

        $this->assertCount(0, $taylor->versions);
        $this->assertEquals(1, SoftDeletesTestUser::whereHas('versions')->count());
    }

There also may be a better name than version for this "test" relationship. Maybe self_referencing() to make the test relation match the test case, making it more clear?

    public function testSelfReferencingRelationshipWithSoftDeletes()
    {
        [$taylor, $abigail] = $this->createUsers();

        $this->assertCount(1, $abigail->self_referencing);
        $this->assertTrue($abigail->self_referencing->first()->is($taylor));

        $this->assertCount(0, $taylor->self_referencing);
        $this->assertEquals(1, SoftDeletesTestUser::whereHas('self_referencing')->count());
    }

    public function self_referencing()
    {
        return $this->hasMany(SoftDeletesTestUser::class, 'user_id')->onlyTrashed();
    }


/**
* Helpers...
*
* @return \Illuminate\Tests\Database\SoftDeletesTestUser[]
*/
protected function createUsers()
{
$taylor = SoftDeletesTestUser::create(['id' => 1, 'email' => '[email protected]']);
$taylor = SoftDeletesTestUser::create(['id' => 1, 'email' => '[email protected]', 'user_id' => 2]);
$abigail = SoftDeletesTestUser::create(['id' => 2, 'email' => '[email protected]']);

$taylor->delete();
Expand Down Expand Up @@ -938,6 +953,11 @@ class SoftDeletesTestUser extends Eloquent
protected $table = 'users';
protected $guarded = [];

public function self_referencing()
{
return $this->hasMany(SoftDeletesTestUser::class, 'user_id')->onlyTrashed();
}

public function posts()
{
return $this->hasMany(SoftDeletesTestPost::class, 'user_id');
Expand Down