How do I do a type cast in Laravel Fluent?

rotaercz picture rotaercz · Mar 25, 2015 · Viewed 9.9k times · Source

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?

Answer

Jeff Lambert picture Jeff Lambert · Mar 25, 2015

Untested, but I believe this should work:

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