PHP code example of ddzobov / laravel-pivot-softdeletes

1. Go to this page and download the library: Download ddzobov/laravel-pivot-softdeletes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ddzobov / laravel-pivot-softdeletes example snippets


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]);