From 42724ae00a7d7fd9635059b229b5ffbbf73b07ef Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Fri, 12 Apr 2019 19:18:06 +0200 Subject: [PATCH] Support withTrashed() on intermediate models --- README.md | 22 ++++++++++++++++++++++ src/Relations/BelongsToThrough.php | 6 +++++- tests/BelongsToThroughTest.php | 23 ++++++++++++++++++++--- tests/Models/Comment.php | 5 +++++ tests/Models/Post.php | 5 +---- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8e569ba..4df5450 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,28 @@ class Comment extends Model } ``` +### Soft Deleting + +By default, soft-deleted intermediate models will be excluded from the result. Use `withTrashed()` to include them: + +```php +class Comment extends Model +{ + use \Znck\Eloquent\Traits\BelongsToThrough; + + public function country() + { + return $this->belongsToThrough('App\Country', ['App\User', 'App\Post']) + ->withTrashed('users.deleted_at'); + } +} + +class User extends Model +{ + use SoftDeletes; +} +``` + ## Contributing Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE OF CONDUCT](CODE_OF_CONDUCT.md) for details. diff --git a/src/Relations/BelongsToThrough.php b/src/Relations/BelongsToThrough.php index 23a6722..0bab652 100644 --- a/src/Relations/BelongsToThrough.php +++ b/src/Relations/BelongsToThrough.php @@ -263,7 +263,7 @@ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['* { return $this->getRelationExistenceQuery($query, $parent, $columns); } - + /** * Restore soft-deleted models. * @@ -274,15 +274,19 @@ public function withTrashed(...$columns) { if (empty($columns)) { $this->query->withTrashed(); + return $this; } + if (is_array($columns[0])) { $columns = $columns[0]; } + $this->query->getQuery()->wheres = collect($this->query->getQuery()->wheres) ->reject(function ($where) use ($columns) { return $where['type'] === 'Null' && in_array($where['column'], $columns); })->values()->all(); + return $this; } diff --git a/tests/BelongsToThroughTest.php b/tests/BelongsToThroughTest.php index d2e3f01..7630185 100644 --- a/tests/BelongsToThroughTest.php +++ b/tests/BelongsToThroughTest.php @@ -4,7 +4,6 @@ use Illuminate\Support\Arr; use Tests\Models\Comment; -use Tests\Models\Post; class BelongsToThroughTest extends TestCase { @@ -17,9 +16,9 @@ public function testLazyLoading() public function testLazyLoadingWithSingleThroughModel() { - $country = Post::first()->country; + $user = Comment::first()->user; - $this->assertEquals(1, $country->id); + $this->assertEquals(11, $user->id); } public function testLazyLoadingWithPrefix() @@ -82,4 +81,22 @@ public function testExistenceQueryWithPrefix() $this->assertEquals([34], Arr::pluck($comments, 'id')); } + + public function testWithTrashed() + { + $user = Comment::find(33)->user() + ->withTrashed() + ->first(); + + $this->assertEquals(13, $user->id); + } + + public function testWithTrashedIntermediate() + { + $country = Comment::find(33)->country() + ->withTrashed('users.deleted_at') + ->first(); + + $this->assertEquals(3, $country->id); + } } diff --git a/tests/Models/Comment.php b/tests/Models/Comment.php index 3647755..d94872c 100644 --- a/tests/Models/Comment.php +++ b/tests/Models/Comment.php @@ -24,4 +24,9 @@ public function countryWithPrefix() { return $this->belongsToThrough(Country::class, [User::class, Post::class], null, 'custom_'); } + + public function user() + { + return $this->belongsToThrough(User::class, Post::class); + } } diff --git a/tests/Models/Post.php b/tests/Models/Post.php index 62d19da..60aaeac 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -4,8 +4,5 @@ class Post extends Model { - public function country() - { - return $this->belongsToThrough(Country::class, User::class); - } + }