Laravel 5.2:Elequent deleted_at Null/!Null 与 where 子句和计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37085141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:44:42  来源:igfitidea点击:

Laravel 5.2 : Elequent deleted_at Null/!Null with where clause and count

phplaravellaravel-5.2

提问by Gammer

I am using eloquent to fetch some records on the basis of condition.

我正在使用 eloquent 根据条件获取一些记录。

$completed = Task::where('user_id', Auth::user()->id)->where('deleted_at', '=', !Null)->get()->count();

$incompleted = Task::where('user_id', Auth::user()->id)->where('deleted_at', '=', Null)->get()->count();

The first one returns 0which is correct. But the second one returns 0which should be 1instead.

第一个返回0,这是正确的。但是第二个返回0应该是1

Table :

桌子 :

Table Data

表格数据

Am i missing something ?

我错过了什么吗?

回答by Matt McDonald

Presuming you're using SoftDeletes in your Task model in a manner similar to this:

假设您在 Task 模型中以类似于以下的方式使用 SoftDeletes:

<?php

namespace App;

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

class Task extends Model
{
    use SoftDeletes;

Then your queries should look like this:

那么您的查询应如下所示:

$completed = Task::where('user_id', Auth::user()->id)->onlyTrashed()->get()->count();

$incompleted = Task::where('user_id', Auth::user()->id)->get()->count();

The onlyTrashed method returns records where deleted_at is filled, but excludes records where deleted_at is null.

onlyTrashed 方法返回已填充deleted_at 的记录,但排除deleted_at 为空的记录。

There is also a withTrashed method that returns records where deleted_at is filled along with records where deleted_at is null.

还有一个 withTrashed 方法,它返回deleted_at 被填充的记录以及deleted_at 为空的记录。

As a general rule, you should avoid querying the deleted_at column directly when using SoftDeletes and instead use the provided methods. By default all records where deleted_at is filled will be excluded.

作为一般规则,在使用 SoftDeletes 时应避免直接查询 deleted_at 列,而应使用提供的方法。默认情况下,所有已填充 delete_at 的记录都将被排除。

You should also make sure you've used the softDeletes() method in your migrations so the column is correctly setup in your database.

您还应该确保在迁移中使用了 softDeletes() 方法,以便在数据库中正确设置该列。

回答by tptcat

Assuming, as you said, you're using soft deletes, you don't need to do anything special to return all non-deleted records:

假设,正如您所说,您正在使用软删除,您不需要做任何特殊的事情来返回所有未删除的记录:

$completed = Task::where('user_id', Auth::user()->id)
                 ->get()
                 ->count(); // according to your DB screenshot this should be '3'

To get onlythe soft deletes, use onlyTrashed():

获取软删除,请使用onlyTrashed()

$notCompleted = Task::where('user_id', Auth::user()->id)
                 ->onlyTrashed()
                 ->get()
                 ->count(); // according to your DB screenshot this should be '2'

To get allrecords:

获取所有记录:

$tasks = Task::where('user_id', Auth::user()->id)
                 ->withTrashed()
                 ->get()
                 ->count(); // according to your DB screenshot this should be '5'

More here: https://laravel.com/docs/5.2/eloquent#querying-soft-deleted-models

更多信息:https: //laravel.com/docs/5.2/eloquent#querying-soft-deleted-models