laravel查出软删除 deleted_at is not null 的内容, laravel database migration

该博客介绍了在 Laravel 框架中如何在查询时排除软删除的记录,通过`withoutGlobalScope(SoftDeletingScope::class)`方法来获取文章回收站中的数据,并展示了创建文章阅读记录的数据库迁移过程,包括字段定义和更新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

\Illuminate\Database\Eloquent\SoftDeletingScope;

$query->withoutGlobalScope(SoftDeletingScope::class)
    ->where($article->getDeletedAtColumn(), "<>", null);
<?php

namespace CmsV2\Services\Articles;

use CmsV2\Models\Article;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\Query\Builder;

class ArticleTrashListService extends ArticlesListService
{
    /**
     * 添加过滤条件
     * @return \Illuminate\Database\Eloquent\Builder
     * @throws \Exception
     */
    public function query() {
        /** @var $query Builder */
        $query = parent::query();
        $article = new Article();
        $query->withoutGlobalScope(SoftDeletingScope::class)
            ->where($article->getDeletedAtColumn(), "<>", null);
        return $query;
    }
}

laravel database migration

php artisan make:migration create_article_read_table

先生成迁移文件./database/migrations/2021_06_17_093830_create_article_read_table.php

补充up方法

        Schema::create('life_cms_article_read', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('content_id')->comment('文章id');
            $table->integer('user_id')->comment('用户id');
            $table->tinyInteger('type')->comment('1.会员用户 2商户用户');
            $table->tinyInteger('read_status')->default(0)->comment('状态 1 已读 0 未读 -1 删除');
            $table->timestamps();
        });

生成表: 

php artisan migrate --path=./database/migrations/2021_06_17_093830_create_article_read_table.php

修改字段后更新表结构

php artisan migrate --path=./database/migrations/2021_08_17_095756_xx_log.php

回滚

php artisan migrate:rollback --step=1

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值