Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel boolean returns "1"/"0" instead of true/false in query

I have a query that returns a boolean field (by the way, I'm on an SQL Server) on my REST API developed with Laravel and a few days ago it was returning the value as true/false, but now it returns that boolean value as String: "1"/"0".

Any idea on what could it be?

I tried using casting on my Model like this:

 protected $casts = [
    'status' => 'boolean',
 ];

This is my query:

return DB::table('dbo.visits as V')
        ->leftJoin('dbo.reports AS R', 'R.id_visit', '=', 'V.id')
        ->select('R.id','R.status'); // status is the boolean

Thanks in advance.

like image 481
guillefix Avatar asked Nov 19 '19 16:11

guillefix


People also ask

Will the “false” === true evaluation prove troublesome when using Boolean validation?

Will the "false" === true evaluation prove troublesome when using this method of boolean validation? Sorry, something went wrong. Yes, it won't work. Validation does not change/coerce the input types.

How to get true and false values from A varchar?

If you want to store the values TRUE / FALSE you would have to use a varchar (5) and the values 'true' and 'false'. Most people, however, use a bit, which allows the values 0 and 1 (and NULL ), however, it does not act like a boolean. I think you need to use Eloquent model for cast to work.

Is it possible to get a boolean value in SQL Server?

SQL Server doesn't have a boolean data type, so you can't have a query that returns one. If you want to store the values TRUE / FALSE you would have to use a varchar (5) and the values 'true' and 'false'. Most people, however, use a bit, which allows the values 0 and 1 (and NULL ), however, it does not act like a boolean.

Is it possible to use Boolean strings in Validation rule?

The way it's phrased now, it isn't very obvious that boolean strings aren't acceptable to use. While technically the validation rule does work with true/false the Middleware workaround posted by @jfadich is required for the rule to work as you would expect based on the docs. Sorry, something went wrong.


2 Answers

When you're defining the following:

protected $casts = [
  'status' => 'boolean',
];

You're defining it on the model. However, when you initiate your query using DB::table(), you're not using the model, so the binding doesn't actually apply. Initiate your query using your model:

return Visit::leftJoin('dbo.reports AS R', 'R.id_visit', '=', 'dbo.visits.id')
->select('R.id','R.status'); // status is the boolean

Note: Had to adjust query to dbo.visits.id from V.id due to aliasing not being available at that point.

like image 171
Tim Lewis Avatar answered Oct 23 '22 19:10

Tim Lewis


Defining casts is working when you are using Eloquent models. In code you provided you use query builder and then Eloquent models are not used at all, so no casts are done.

like image 27
Marcin Nabiałek Avatar answered Oct 23 '22 19:10

Marcin Nabiałek