Add this lines to composer.json
:
"repositories": [
{
"type": "github",
"url": "https://github.com/diorit-techno/laravel-pivot-softdeletes.git"
}
],
Require this package with composer:
composer require ddzobov/laravel-pivot-softdeletes
use DDZobov\PivotSoftDeletes\Model;
class Post extends Model
{
public function tags()
{
return $this->belongsToMany(Tag::class)->withSoftDeletes();
}
}
class Tag extends Model
{
public function posts()
{
return $this->belongsToMany(Post::class)->withSoftDeletes();
}
}
use DDZobov\PivotSoftDeletes\Model;
use DDZobov\PivotSoftDeletes\Relations\Pivot;
class Post extends Model
{
public function tagsWithCustomPivot()
{
return $this->belongsToMany(Tag::class)->using(PostTag::class)->withSoftDeletes();
}
}
class Tag extends Model
{
public function postsWithCustomPivot()
{
return $this->belongsToMany(Post::class)->using(PostTag::class)->withSoftDeletes();
}
}
class PostTag extends Pivot
{
}
$this->belongsToMany(Post::class)->withSoftDeletes('custom_deleted_at');
// withoutTrashed() already called inside withSoftDeletes()
$this->belongsToMany(Post::class)->withSoftDeletes();
// same behavior
$this->belongsToMany(Post::class)->withSoftDeletes()->withoutTrashedPivots();
$this->belongsToMany(Post::class)->withSoftDeletes()->withTrashedPivots();
$this->belongsToMany(Post::class)->withSoftDeletes()->onlyTrashedPivots();
$post->tags()->restore([$tag->id]);
$post->tagsWithCustomPivot()->restore([$tag->id]);
$post->tags()->forceDetach([$tag->id]);
$post->tags()->syncWithForceDetaching([$tag->id]);
composer test
The MIT License (MIT). Please see License File for more information.