如何在 Laravel Fluent 中进行类型转换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29265572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:13:18  来源:igfitidea点击:

How do I do a type cast in Laravel Fluent?

phpmysqllaravelfluent

提问by rotaercz

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

如何在 Laravel Fluent 中进行类型转换以比较值?例如,如果我有以下 MySQL:

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

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

这就是我在 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?

目前,数据库将其视为字符串。由于其他原因,表中的列需要保留为 varchar。如何在 Laravel Fluent 中为这个特定查询进行类型转换?

回答by Jeff Lambert

Untested, but I believe this should work:

未经测试,但我相信这应该有效:

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

回答by dani24

Also

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

works

作品