1

I'm using Laravel Framework (8.26) and PHP 8.0.5

My database is Postgresql (12.3)

This is my schema (cities table):

Column Type Collation Nullable Default
id bigint not null
is_active boolean not null true

It also has lots of other fields which are not relevant to my question.

I've created a model for this table:

class City extends Model
{
    public $timestamps = false;
    protected $table = 'cities';
}

Then when I use the query builder:

City::where('is_active', true)->get();

It raises an error:

Illuminate\Database\QueryException
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: boolean = integer LINE 1: select * from "cities" where "is_active" = $1 ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts. (SQL: select * from "cities" where "is_active" = 1)

Why does it cast the field from boolean to integer? As you see, I explicitly passed true in second argument but Laravel converts the value to 1 and Postgres returns an error. However, when I run the raw query with "is_active = true" it works.

2 Answers 2

3
City::where('is_active','=' ,'true')->get();
Sign up to request clarification or add additional context in comments.

4 Comments

It solved my problem but how? Why should we use 'true' as a string instead of true as a boolean ?!
If you're using booleans while inserting or updating data, laravel will convert them, but it can't do that while fetching records because it has no knowledge of the data types it reads - @FarhadKousha
We're facing the same issues, this is quite absurd because we already have a huge system and now this is will need for us to do some refactor.
2

This was caused by an update to php 8.0.5. You need to downgrade to PHP 8.0.4 or wait for 8.0.6. https://github.com/laravel/framework/issues/37215

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.