澄清一下你的观点。
1. 当我们在代码中使用软删除时,删除时间戳将从null更新为当前时间戳,因此在从项目中的任何地方查询该特定表格的数据时,Eloquent模型将自动返回已删除时间戳设置为NULL的数据,即它不是软删除。
people using something called static::deleting.
you might have seen somewhat like this example if I am understanding your question in right way:
class X-Model extends Eloquent
{
public function xy()
{
return $this->has_many('XY_Model');
}
protected static function boot() {
parent::boot();
static::deleting(function($x) {
$user->xy()->delete();
});
}
}
This is a use-case for Eloquent events to delete a record which will use the "deleting" event to do the cleanup.
delete data using soft delete
现在,在调用此函数后,您可以从表中软删除数据,如果在模型中正确维护了关系,则还可以从其他相关表中软删除数据。这实际上被称为级联效应。以下是一个示例,以便您更好地了解。
模型结构:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Utilities\Uuids;
use Illuminate\Database\Eloquent\SoftDeletes;
use Iatstuti\Database\Support\CascadeSoftDeletes;
class XFolder extends Model
{
use SoftDeletes, CascadeSoftDeletes;
use Uuids;
protected $connection = 'XDB';
protected $table = 'x_folder';
protected $dates = ['deleted_at'];
public $incrementing = false;
protected $cascadeDeletes = ['XReference'];
protected $visible = [ 'id', 'x_id', 'xz_id', 'at_id', 'title', 'description', 'description', 'status', 'created_on' , 'is_active'];
protected $fillable = [
'x_id',
'xz_id',
'at_id',
'title',
'description',
'status',
'created_on',
'is_active'
];
public function XReference()
{
return $this->hasMany('App\Models\XReference', 'x_id');
}
}
现在你可以在控制器中这样调用:
public function deleteData($id)
{
$results = $this->getModel()->where('id', $id)->deleteData();
return $results;
}
这将会删除数据(以级联方式进行软删除)。