Skip to content

Commit

Permalink
Laravel 7.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
ddzobov committed Apr 30, 2020
1 parent 2a6c296 commit 60a379d
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 1,216 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Laravel pivot SoftDeletes for Laravel 5.8 & 6.x
# Laravel pivot SoftDeletes for Laravel 5.8 & 6.x & 7.x

## Installation

Expand Down Expand Up @@ -69,17 +69,17 @@ $this->belongsToMany(Post::class)->withSoftDeletes('custom_deleted_at');
$this->belongsToMany(Post::class)->withSoftDeletes();

// same behavior
$this->belongsToMany(Post::class)->withSoftDeletes()->withoutTrashed();
$this->belongsToMany(Post::class)->withSoftDeletes()->withoutTrashedPivots();
```

### Show exists & trashed:
```php
$this->belongsToMany(Post::class)->withSoftDeletes()->withTrashed();
$this->belongsToMany(Post::class)->withSoftDeletes()->withTrashedPivots();
```

### Show only trashed:
```php
$this->belongsToMany(Post::class)->withSoftDeletes()->onlyTrashed();
$this->belongsToMany(Post::class)->withSoftDeletes()->onlyTrashedPivots();
```

### Restore pivot recods:
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
}
],
"require": {
"illuminate/database": "^5.8|^6.0"
"illuminate/database": "^5.8|^6.0|^7.0"
},
"require-dev": {
"laravel/framework": "^5.8|^6.0",
"mockery/mockery": "^1.0",
"orchestra/testbench-core": "3.8.*",
"phpunit/phpunit": "^7.5|^8.0"
"laravel/framework": "^5.8|^6.0|^7.0",
"mockery/mockery": "^1.3.1",
"orchestra/testbench-core": "^5.0",
"phpunit/phpunit": "^8.4|^9.0"
},
"autoload": {
"psr-4": {
Expand Down
28 changes: 14 additions & 14 deletions src/Relations/BelongsToManySoft.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* @method self withoutTrashed() Show only non-trashed records
* @method self withTrashed() Show all records
* @method self onlyTrashed() Show only trashed records
* @method int forceDetach(\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids, bool $touch) Show only trashed records
* @method int syncWithForceDetaching(mixed $ids) Show only trashed records
* @method self withoutTrashedPivots() Show only non-trashed records
* @method self withTrashedPivots() Show all records
* @method self onlyTrashedPivots() Show only trashed records
* @method int forceDetach(\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids, bool $touch) Force detach records
* @method int syncWithForceDetaching(mixed $ids) Sync many-to-many relationship with force detaching
*/
class BelongsToManySoft extends BelongsToMany
{
Expand Down Expand Up @@ -67,28 +67,28 @@ public function withSoftDeletes($deletedAt = 'deleted_at')

$this->pivotDeletedAt = $deletedAt;

$this->macro('withoutTrashed', function () {
$this->query->withGlobalScope('withoutTrashed', function (Builder $query) {
$this->macro('withoutTrashedPivots', function () {
$this->query->withGlobalScope('withoutTrashedPivots', function (Builder $query) {
$query->whereNull(
$this->getQualifiedDeletedAtColumnName()
);
})->withoutGlobalScopes(['onlyTrashed']);
})->withoutGlobalScopes(['onlyTrashedPivots']);

return $this;
});

$this->macro('withTrashed', function () {
$this->query->withoutGlobalScopes(['withoutTrashed', 'onlyTrashed']);
$this->macro('withTrashedPivots', function () {
$this->query->withoutGlobalScopes(['withoutTrashedPivots', 'onlyTrashedPivots']);

return $this;
});

$this->macro('onlyTrashed', function () {
$this->query->withGlobalScope('onlyTrashed', function (Builder $query) {
$this->macro('onlyTrashedPivots', function () {
$this->query->withGlobalScope('onlyTrashedPivots', function (Builder $query) {
$query->whereNotNull(
$this->getQualifiedDeletedAtColumnName()
);
})->withoutGlobalScopes(['withoutTrashed']);
})->withoutGlobalScopes(['withoutTrashedPivots']);

return $this;
});
Expand All @@ -109,7 +109,7 @@ public function withSoftDeletes($deletedAt = 'deleted_at')
});
});

return $this->withPivot($this->deletedAt())->withoutTrashed();
return $this->withPivot($this->deletedAt())->withoutTrashedPivots();
}

/**
Expand Down
Loading

0 comments on commit 60a379d

Please sign in to comment.