软删除是通过添加“已删除”标记而非真正删除数据来保留记录,laravel 提供内置支持。1. 在模型中引入 softdeletes trait 并指定 deleted_at 为日期类型;2. 创建迁移文件使用 softdeletes() 方法添加 deleted_at 字段;3. 调用 delete() 方法实现软删除,forcedelete() 可彻底删除;4. 查询时默认排除软删除记录,withtrashed() 包含所有记录,onlytrashed() 仅包含已软删除记录;5. 恢复记录使用 restore() 方法;6. 自定义字段名需在模型定义 $deletedat 属性并在迁移中创建对应字段;7. 关联查询可通过 withtrashed() 包含软删除记录;8. 全局禁用软删除过滤可调用 reguard(),操作完成后应重新启用作用域。
软删除,简单来说,就是给你的数据表加上一个“已删除”的标记,而不是真的从数据库里删掉。这样,你就可以恢复误删的数据,或者保留历史记录。laravel 内置了对软删除的支持,用起来相当方便。
实现软删除,主要涉及到模型修改、数据库迁移,以及查询时的过滤。
解决方案
-
添加 SoftDeletes Trait 到你的模型:
在你的模型类中引入 SoftDeletes trait。例如,如果你的模型是 AppModelsPost,就这样写:
<?php namespace AppModels; use IlluminateDatabaseEloquentModel; use IlluminateDatabaseEloquentSoftDeletes; class Post extends Model { use SoftDeletes; protected $dates = ['deleted_at']; // 可选,但推荐,指定 deleted_at 为日期类型 }
$dates 属性用于告诉 Eloquent deleted_at 字段应该被视为日期类型,这样你就可以使用 carbon 对象来处理它。
-
创建数据库迁移:
如果你的数据表还没有 deleted_at 字段,你需要创建一个迁移来添加它。运行:
php artisan make:migration add_deleted_at_to_posts_table --table=posts
然后,在生成的迁移文件中,修改 up() 方法:
<?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class AddDeletedAtToPostsTable extends Migration { public function up() { Schema::table('posts', function (Blueprint $table) { $table->softDeletes(); }); } public function down() { Schema::table('posts', function (Blueprint $table) { $table->dropSoftDeletes(); }); } }
运行迁移:
php artisan migrate
这会在 posts 表中添加一个 deleted_at 字段,类型为 timestamp。
-
使用软删除:
现在,你可以在你的模型中使用 delete() 方法进行软删除:
$post = Post::find(1); $post->delete(); // 这不会真的删除记录,而是设置 deleted_at 字段
要永久删除记录,可以使用 forceDelete() 方法:
$post->forceDelete(); // 这会真的从数据库中删除记录
-
查询软删除的数据:
默认情况下,Eloquent 查询会排除软删除的记录。如果你想包含软删除的记录,可以使用以下方法:
-
withTrashed():包含所有记录,包括软删除的。
$posts = Post::withTrashed()->get();
-
onlyTrashed():只包含软删除的记录。
$posts = Post::onlyTrashed()->get();
-
restore():恢复软删除的记录。
$post = Post::withTrashed()->find(1); $post->restore(); // 恢复 deleted_at 为 null
-
如何自定义 deleted_at 字段的名称?
默认情况下,Laravel 假设你的软删除字段名为 deleted_at。如果你想使用不同的字段名,可以在你的模型中定义一个 $deletedAt 属性:
<?php namespace AppModels; use IlluminateDatabaseEloquentModel; use IlluminateDatabaseEloquentSoftDeletes; class Post extends Model { use SoftDeletes; protected $dates = ['custom_deleted_at']; protected $deletedAt = 'custom_deleted_at'; }
然后,在你的数据库迁移中,创建名为 custom_deleted_at 的字段。
软删除对关联关系有什么影响?
使用软删除时,需要注意关联关系的处理。默认情况下,Eloquent 会自动过滤掉软删除的关联记录。例如,如果 Post 模型有一个 comments() 关联关系,并且 Comment 模型也使用了软删除,那么在查询 Post 的 comments 时,只会返回未被软删除的评论。
如果你想包含软删除的关联记录,可以使用 withTrashed() 方法:
$post = Post::find(1); $comments = $post->comments()->withTrashed()->get();
或者,你可以在关联关系定义中使用 withTrashed():
public function comments() { return $this->hasMany(Comment::class)->withTrashed(); }
如何在全局范围内禁用软删除的过滤?
有时候,你可能需要在整个应用中禁用软删除的过滤。例如,你可能需要编写一些管理工具,可以访问所有记录,包括软删除的记录。
要做到这一点,你可以使用 Model::reguard() 方法:
Post::reguard(); // 禁用 Post 模型上的所有全局作用域,包括软删除 $posts = Post::all(); // 现在 $posts 包含所有记录,包括软删除的 Post::unguard(false); // 重新启用全局作用域
但是,请谨慎使用此方法,因为它可能会导致意外的行为。最好只在必要时使用,并在完成后立即重新启用全局作用域。