Suppose I have Post
model that has is_verified
column with smallint
datatype, how can I get all records that is verified? One thing to do this is using this:
Post::where('is_verified', true)->get();
The code above will produce the following query:
select * from `posts` where `posts`.`is_verified` = true
... which will get me all verified Post
records; in note that is_verified
on all existing records is either 0
or 1
.
However, after I get myself curious and try to manually change some is_verified
's record value from 1
to another truthy number e.g. 2
, the above eloquent query didn't work as expected anymore: records with is_verified
value of 2
didn't get retrieved.
I tried to execute the sql query directly from HeidiSQL as well, but it was just the same. Then I tried to change the =
in the sql query to is
, and now it's working as expected i.e. all records with truthy is_verified
get retrieved:
select * from `posts` where `posts`.`is_verified` is true
So my questions are:
- Does the above behaviour is correct and expected?
- How can I execute the last sql query in eloquent? One thing I can think of is
where('is_verified', '!=', 0)
but that feels weird in terms of readability especially when the query is pretty long and a bit complicated - As I stated before, the
is_verified
column is asmallint
. Does this affects the behaviour? Because this conversation here states thatboolean
column datatype is typicallytinyint
, notsmallint
.
And that's it. Thank you in advance!