Search code examples
laravelcollectionseloquentboolean

Laravel eloquent not retrieving results based on boolean column


I have 33 results on my DB where this boolean field (resolvido) is showing zero, if I run the SQL query it retrieves the results, however in the model the collection always comes empty.

If I change to Model::where('resolvido',1)->get(), I do get the correspondent results, however the Model::where('resolvido',0)->get() or the Model::where('resolvido',false)->get() or even using the query builder and raw sql, the collection never picks up those 33 results where resolvido = 0.

I've also tried in Tinker and it always comes up empty.

Migration:

        $table->boolean('resolvido')->nullable();   

On DB comes up as TINYINT

Any tips on what it might be?

Thanks in advance


Solution

  • Naturally, there is no boolean data type for MySQL but rather uses TINYINT (0 or 1) for this.

    Laravel's query builder, allows using boolean in the where clause. You might want to try these:

    Model::where('resolved', true)->get();
    Model::where('resolved', false)->get();
    

    Since you've set the resolved column in the DB as nullable, you might also want to try:

    Model::where('resolved', null)->get();
    

    If you don't want that behavior use $table->boolean('resolved')->default(false) instead