Laravel Softdelete具有外键和限制约束?

时间:2022-10-18 15:00:00

I was wondering how to use Laravels softdelete with something like MySql foreign key restrict constraints.

我想知道如何使用Laravels softdelete与MySql外键限制约束。

Is there already something build into the framework? Softdelete is already working but I need some kind of validation to related models. e.g. an error message to the user "You cannot delete this item because it hast 5 related records"

框架中是否已有内置功能? Softdelete已经在运行,但我需要对相关模型进行某种验证。例如向用户显示错误消息“您无法删除此项目,因为它有5条相关记录”

Thanks,

谢谢,

2 个解决方案

#1


0  

Yes, in Laravel they have soft delete models.To use this Illuminate\Database\Eloquent\SoftDeletes trait on the model and add the deleted_at column to your $dates property.

是的,在Laravel中他们有软删除模型。要在模型上使用此Illuminate \ Database \ Eloquent \ SoftDeletes特征,并将deleted_at列添加到$ dates属性。

They also have their Querying Soft Deleted Models.You can choose anyone.

他们也有他们的查询软删除模型。你可以选择任何人。

You can get all documentation about this in here.

您可以在此处获取有关此内容的所有文档。

#2


0  

There's is no build in solution for "unpluging" DB relation on soft delete.

在软删除中“没有”删除DB关系的解决方案没有内置。

What You can do for example is to use observers (event listener) or use eloquent model events for example deleting/deleted like:

例如,您可以使用观察者(事件监听器)或使用雄辩的模型事件,例如删除/删除,如:

public function boot()
{
    User::deleted(function ($user) {
        if ($user->deleted_at) {
            // here You have to unplug all the dependencies
        }
    });
}

#1


0  

Yes, in Laravel they have soft delete models.To use this Illuminate\Database\Eloquent\SoftDeletes trait on the model and add the deleted_at column to your $dates property.

是的,在Laravel中他们有软删除模型。要在模型上使用此Illuminate \ Database \ Eloquent \ SoftDeletes特征,并将deleted_at列添加到$ dates属性。

They also have their Querying Soft Deleted Models.You can choose anyone.

他们也有他们的查询软删除模型。你可以选择任何人。

You can get all documentation about this in here.

您可以在此处获取有关此内容的所有文档。

#2


0  

There's is no build in solution for "unpluging" DB relation on soft delete.

在软删除中“没有”删除DB关系的解决方案没有内置。

What You can do for example is to use observers (event listener) or use eloquent model events for example deleting/deleted like:

例如,您可以使用观察者(事件监听器)或使用雄辩的模型事件,例如删除/删除,如:

public function boot()
{
    User::deleted(function ($user) {
        if ($user->deleted_at) {
            // here You have to unplug all the dependencies
        }
    });
}