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.

STA
  • 30,729
  • 8
  • 45
  • 59
Farhad Kousha
  • 13
  • 1
  • 5

2 Answers2

3
City::where('is_active','=' ,'true')->get();
JEJ
  • 814
  • 5
  • 21
  • It solved my problem but how? Why should we use 'true' as a string instead of true as a boolean ?! – Farhad Kousha May 03 '21 at 10:08
  • 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 – JEJ May 03 '21 at 10:11
  • @FarhadKousha https://stackoverflow.com/a/60751986/15648920 – JEJ May 03 '21 at 10:17
  • 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. – Fabio William Conceição May 05 '21 at 09:55
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

tobigumo
  • 36
  • 1