如何在 Laravel eloquent 中使用 substr?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43772915/
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 15:54:41  来源:igfitidea点击:

How can I use substr in laravel eloquent?

phplaravellaravel-5.3laravel-eloquent

提问by samuel toh

I use Laravel 5.3

我使用 Laravel 5.3

My laravel eloquent is like this :

我的 Laravel 口才是这样的:

$query = User::where('year', '=', (string)$object->year)               
             ->where(\DB::raw('substr(code, 1, 3)'), '=', 511)
             ->get();

I try like that, but it does not work

我这样尝试,但它不起作用

How can I solve it?

我该如何解决?

回答by PHP Dev

you forgot to put comma after '=' sign. try this .

你忘记在 '=' 符号后放逗号。尝试这个 。

   $query = User::where('year', '=', (string)$object->year)               
           ->where(\DB::raw('substr(code, 1, 3)'), '=' , 511)
           ->get();

回答by Nicholas Seetaram

Late on this thread but for anyone who is having the same issue with the aforementioned topic i found a solution by concatenating the value. For example :-

在此线程上较晚,但对于与上述主题有相同问题的任何人,我通过连接值找到了解决方案。例如 :-

return User::whereRaw('SUBSTRING(column, -2,  2) = '.$value)->get();

Hope this helps.

希望这可以帮助。