ddzobov / laravel-pivot-softdeletes by zobov

Make your Eloquent models pivots be able to soft deleted in Laravel/Lumen
218,112
55
4
Package Data
Maintainer Username: zobov
Maintainer Contact: ddzobov@gmail.com (Daniil Zobov)
Package Create Date: 2020-01-16
Package Last Update: 2024-03-21
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-26 03:26:57
Package Statistics
Total Downloads: 218,112
Monthly Downloads: 8,506
Daily Downloads: 264
Total Stars: 55
Total Watchers: 4
Total Forks: 17
Total Open Issues: 9

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.