Skip to content

Commit

Permalink
Support withTrashed() on intermediate models
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Apr 12, 2019
1 parent 8d84330 commit 42724ae
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*
{
return $this->getRelationExistenceQuery($query, $parent, $columns);
}

/**
* Restore soft-deleted models.
*
Expand All @@ -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;
}

Expand Down
23 changes: 20 additions & 3 deletions tests/BelongsToThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Arr;
use Tests\Models\Comment;
use Tests\Models\Post;

class BelongsToThroughTest extends TestCase
{
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions tests/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 1 addition & 4 deletions tests/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@

class Post extends Model
{
public function country()
{
return $this->belongsToThrough(Country::class, User::class);
}

}

0 comments on commit 42724ae

Please sign in to comment.