Eloquent ORM 实例教程 —— 模型删除及软删除相关实现


guide-to-eloquent-orm

1、删除模型

1.1 使用delete删除模型

删除模型很简单,先获取要删除的模型实例,然后调用delete方法即可:

$post = Post::find(5);
if($post->delete()){
    echo '删除文章成功!';
}else{
    echo '删除文章失败!';
}

该方法返回truefalse

1.2 使用destroy删除模型

当然如果已知要删除的模型id的话,可以用更简单的方法destroy直接删除:

$deleted = Post::destroy(5);

你也可以一次传入多个模型id删除多个模型:

$deleted = Post::destroy([1,2,3,4,5]);

调用destroy方法返回被删除的记录数。

1.3 使用查询构建器删除模型

既然前面提到Eloquent模型本身就是查询构建器,也可以使用查询构建器风格删除模型,比如我们要删除所有浏览数为0的文章,可以使用如下方式:

$deleted = Models\Post::where('views', 0)->delete();

返回结果为被删除的文章数。

2、软删除及其相关实现

2.1 软删除实现

上述删除方法都会将数据表记录从数据库删除,此外Eloquent模型还支持软删除。

所谓软删除指的是数据表记录并未真的从数据库删除,而是将表记录的标识状态标记为软删除,这样在查询的时候就可以加以过滤,让对应表记录看上去是被”删除“了。Laravel中使用了一个日期字段作为标识状态,这个日期字段可以自定义,这里我们使用deleted_at,如果对应模型被软删除,则deleted_at字段的值为删除时间,否则该值为空。

要让Eloquent模型支持软删除,还要做一些设置。首先在模型类中要使用SoftDeletestrait,该trait为软删除提供一系列相关方法,具体可参考源码Illuminate\Database\Eloquent\SoftDeletes,此外还要设置$date属性数组,将deleted_at置于其中:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;

    //设置表名
    public $table = 'posts';

    //设置主键
    public $primaryKey = 'id';

    //设置日期时间格式
    public $dateFormat = 'U';

    protected $guarded = ['id','views','user_id','updated_at','created_at'];

    protected $dates = ['delete_at'];
}

然后对应的数据库posts中添加deleted_at列,我们使用迁移来实现,先执行Artisan命令:

php artisan make:migration alter_posts_deleted_at --table=posts

然后编辑生成的PHP文件如下:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterPostsDeletedAt extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->softDeletes();
        });
    }

    ...//其它方法
}

然后运行:

php artisan migrate

这样posts中就有了deleted_at列。接下来,我们在控制器中编写测试代码:

$post = Post::find(6);
$post->delete();
if($post->trashed()){
    echo '软删除成功!';
    dd($post);
}else{
    echo '软删除失败!';
}

在浏览器中访问http://laravel.app:8000/test,页面输出如下:

软删除后模型不显示

当我们再次通过下面这段代码获取所有文章:

$posts = Post::all();
dd($posts);

已经看不到id为6的文章的身影了。

2.2 查询结果包含软删除模型

那如果想要在查询结果中包含软删除的记录呢?可以使用SoftDeletes trait上的withTrashed方法:

$posts = Post::withTrashed()->get();
dd($posts);

执行之后页面显示如下:

查询结果包含软删除模型

id为6的文章又出现在了查询结果中。有时候我们只想要查看被软删除的模型,这也有招,通过SoftDeletes上的onlyTrashed方法即可:

$posts = Post::onlyTrashed()->get();
dd($posts);

执行后页面显示结果如下:

查询结果只包含软删除模型

2.3 软删除恢复

有时候我们需要恢复被软删除的模型,可以使用SoftDeletes提供的restore方法:

恢复单个模型

$post = Post::find(6);
$post->restore();

恢复多个模型

Post::withTrashed()->where('id','>',1)->restore();

恢复所有模型

Post::withTrashed()->restore();

恢复关联查询模型

$post = Post::find(6);
$post->history()->restore();

2.4 强制删除

如果模型配置了软删除但我们确实要删除改模型对应数据库表记录,则可以使用SoftDeletes提供的forceDelete方法:

$post = Post::find(6);
$post->forceDelete();

查看数据表可以发现id=6的表记录已经被删除,不复存在:

使用强制删除后的结果集

该方法也支持关联模型强制删除,我们后面讲到关联关系的时候再具体说明。


点赞 取消点赞 收藏 取消收藏

<< 上一篇: Eloquent ORM 实例教程 —— 模型创建、更新及批量赋值

>> 下一篇: Eloquent ORM 实例教程 —— 查询作用域和模型事件