Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do a type cast in Laravel Fluent?

How can I do a type cast for comparing values in Laravel Fluent? For example if I have the following MySQL:

SELECT * from table1 WHERE  CAST(`values` AS SIGNED) > $myVar

This is what I currently have after writing the above in Fluent:

$query = DB::connection('mysql')->table('table1')
    ->where('values', '>', $myVar);

Currently the database is treating this as a string. The column in the table needs to be kept as a varchar for other reasons. How can I do the type cast for this particular query in Laravel Fluent?

like image 292
rotaercz Avatar asked Dec 24 '22 21:12

rotaercz


2 Answers

Untested, but I believe this should work:

$query = DB::connection('mysql')->table('table1')
    ->where(DB::raw('CAST(values AS SIGNED)'), '>', $myVar);
like image 166
Jeff Lambert Avatar answered Dec 27 '22 10:12

Jeff Lambert


Also

$query= DB::connection('mysql')
            ->table('table1')
            ->whereRaw('CAST(values AS SIGNED) > '.$myVar);

works

like image 26
dani24 Avatar answered Dec 27 '22 09:12

dani24