Shafee Shafee - 6 months ago 49
PHP Question

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

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

$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
0
which is correct.
But the second one returns
0
which should be
1
instead.

Table :

Table Data

Am i missing something ?

Answer

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();