ddzobov / laravel-pivot-softdeletes by zobov

Make your Eloquent models pivots be able to soft deleted in Laravel/Lumen
274,347
61
4
Package Data
Maintainer Username: zobov
Maintainer Contact: ddzobov@gmail.com (Daniil Zobov)
Package Create Date: 2020-01-16
Package Last Update: 2024-07-15
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:23:00
Package Statistics
Total Downloads: 274,347
Monthly Downloads: 8,563
Daily Downloads: 321
Total Stars: 61
Total Watchers: 4
Total Forks: 18
Total Open Issues: 7

Laravel pivot SoftDeletes for Laravel 5 & 6

Installation

Require this package with composer:

composer require ddzobov/laravel-pivot-softdeletes

Basic usage

New models:

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();
    }
}

Existing models:

use Illuminate\Database\Eloquent\Model;
use DDZobov\PivotSoftDeletes\Concerns\HasRelationships;

class Post extends Model
{
    use HasRelationships;

    public function tags()
    {
        return $this->belongsToMany(Tag::class)->withSoftDeletes();
    }
}

class Tag extends Model
{
    use HasRelationships;

    public function posts()
    {
        return $this->belongsToMany(Post::class)->withSoftDeletes();
    }
}

New pivot model:

use DDZobov\PivotSoftDeletes\Model;
use DDZobov\PivotSoftDeletes\Relations\Pivot;

class Post extends Model
{
    public function tags()
    {
        return $this->belongsToMany(Tag::class)->using(PostTag::class)->withSoftDeletes();
    }
}

class Tag extends Model
{
    public function posts()
    {
        return $this->belongsToMany(Post::class)->using(PostTag::class)->withSoftDeletes();
    }
}

class PostTag extends Pivot
{
    
}

Existing pivot models:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
use DDZobov\PivotSoftDeletes\Concerns\HasRelationships;

class Post extends Model
{
    use HasRelationships;

    public function tags()
    {
        return $this->belongsToMany(Tag::class)->using(PostTag::class)->withSoftDeletes();
    }
}

class Tag extends Model
{
    use HasRelationships;

    public function posts()
    {
        return $this->belongsToMany(Post::class)->using(PostTag::class)->withSoftDeletes();
    }
}

class PostTag extends Pivot
{
    use SoftDeletes;
}

Custom deleted_at field:

$this->belongsToMany(Post::class)->withSoftDeletes('custom_deleted_at');

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.