Skip to content

Commit

Permalink
[10.x] Allow utilising withTrashed(), withoutTrashed() and `onlyT…
Browse files Browse the repository at this point in the history
…rashed()` on `MorphTo` relationship even without `SoftDeletes` Model (#47880)

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Update tests/Integration/Database/EloquentMorphEagerLoadingTest.php

* formatting

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
crynobone and taylorotwell authored Aug 15, 2023
1 parent 14e8ee4 commit 294661d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,57 @@ public function constrain(array $callbacks)
return $this;
}

/**
* Indicate that soft deleted models should be included in the results.
*
* @return $this
*/
public function withTrashed()
{
$callback = fn ($query) => $query->hasMacro('withTrashed') ? $query->withTrashed() : $query;

$this->macroBuffer[] = [
'method' => 'when',
'parameters' => [true, $callback],
];

return $this->when(true, $callback);
}

/**
* Indicate that soft deleted models should not be included in the results.
*
* @return $this
*/
public function withoutTrashed()
{
$callback = fn ($query) => $query->hasMacro('withoutTrashed') ? $query->withoutTrashed() : $query;

$this->macroBuffer[] = [
'method' => 'when',
'parameters' => [true, $callback],
];

return $this->when(true, $callback);
}

/**
* Indicate that only soft deleted models should be included in the results.
*
* @return $this
*/
public function onlyTrashed()
{
$callback = fn ($query) => $query->hasMacro('onlyTrashed') ? $query->onlyTrashed() : $query;

$this->macroBuffer[] = [
'method' => 'when',
'parameters' => [true, $callback],
];

return $this->when(true, $callback);
}

/**
* Replay stored macro calls on the actual related instance.
*
Expand Down
42 changes: 42 additions & 0 deletions tests/Integration/Database/EloquentMorphEagerLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
Expand All @@ -14,6 +15,7 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->softDeletes();
});

Schema::create('posts', function (Blueprint $table) {
Expand All @@ -25,20 +27,30 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
$table->increments('video_id');
});

Schema::create('actions', function (Blueprint $table) {
$table->increments('id');
$table->string('target_type');
$table->integer('target_id');
});

Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('commentable_type');
$table->integer('commentable_id');
});

$user = User::create();
$user2 = User::forceCreate(['deleted_at' => now()]);

$post = tap((new Post)->user()->associate($user))->save();

$video = Video::create();

(new Comment)->commentable()->associate($post)->save();
(new Comment)->commentable()->associate($video)->save();

(new Action)->target()->associate($video)->save();
(new Action)->target()->associate($user2)->save();
}

public function testWithMorphLoading()
Expand All @@ -49,9 +61,13 @@ public function testWithMorphLoading()
}])
->get();

$this->assertCount(2, $comments);

$this->assertTrue($comments[0]->relationLoaded('commentable'));
$this->assertInstanceOf(Post::class, $comments[0]->getRelation('commentable'));
$this->assertTrue($comments[0]->commentable->relationLoaded('user'));
$this->assertTrue($comments[1]->relationLoaded('commentable'));
$this->assertInstanceOf(Video::class, $comments[1]->getRelation('commentable'));
}

public function testWithMorphLoadingWithSingleRelation()
Expand All @@ -65,6 +81,30 @@ public function testWithMorphLoadingWithSingleRelation()
$this->assertTrue($comments[0]->relationLoaded('commentable'));
$this->assertTrue($comments[0]->commentable->relationLoaded('user'));
}

public function testMorphLoadingMixedWithTrashedRelations()
{
$action = Action::query()
->with('target')
->get();

$this->assertCount(2, $action);

$this->assertTrue($action[0]->relationLoaded('target'));
$this->assertInstanceOf(Video::class, $action[0]->getRelation('target'));
$this->assertTrue($action[1]->relationLoaded('target'));
$this->assertInstanceOf(User::class, $action[1]->getRelation('target'));
}
}

class Action extends Model
{
public $timestamps = false;

public function target()
{
return $this->morphTo()->withTrashed();
}
}

class Comment extends Model
Expand All @@ -90,6 +130,8 @@ public function user()

class User extends Model
{
use SoftDeletes;

public $timestamps = false;
}

Expand Down

0 comments on commit 294661d

Please sign in to comment.