<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mlezcano1985 / laravel-pivot-soft-deletes example snippets
namespace App;
use Mlezcano1985\Database\Support\PivotSoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
class Account extends Authenticatable
{
use Notifiable, SoftDeletes, PivotSoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
}
/**
* @return BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(Role::class);
}
/**
* Automatically creates hash for the user password.
*
* @param string $value
* @return void
*/
public function setPasswordAttribute($value)
{
$this->attributes['password'] = Hash::make($value);
}
}
namespace App;
use Mlezcano1985\Database\Support\PivotSoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
use SoftDeletes, PivotSoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
}
/**
* @return BelongsToMany
*/
public function accounts()
{
return $this->belongsToMany(Account::class);
}
}
$account = App\Role::find($role_id)->accounts()->findOrFail($account_id)
$account->detach(); // Soft delete the Intermediate Table
/**
* @return BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(Role::class)->using(AccountRole::class);
}
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
class AccountRole extends Pivot
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
}
}
$account = App\Role::find($role_id)->accounts()->findOrFail($account_id)
$account->detach(); // Soft delete the Intermediate Table
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.