laravel eloquent查询集合中字段的第一个字母

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

laravel eloquent query first letter of field in collection

phplaravel

提问by ONYX

Does any one know how to query in a collection using eloquent using the where statement to see if the first letter in a field matches the queried letter. If someone know of another way to do this other than eloquent could you leave me an example thanks

有谁知道如何使用 eloquent 使用 where 语句在集合中查询,以查看字段中的第一个字母是否与查询的字母匹配。如果有人知道除了雄辩之外的另一种方式来做到这一点,你能给我一个例子吗谢谢

what I have is just a simple query which just gets a collection but I want to query the first letter and select only those which matches in the query

我所拥有的只是一个简单的查询,它只是获取一个集合,但我想查询第一个字母并只选择那些在查询中匹配的字母

$tags = Tag::orderBy('name', 'asc')->paginate(40);

Need to be like

需要像

$tags = Tag::orderBy('name', 'asc')->where(first letter', '=', 'L')->paginate(40);

回答by ToothlessRebel

Use the SQL LIKEcondition.

使用 SQLLIKE条件。

->where($field, 'LIKE', $letter.'%');

->where($field, 'LIKE', $letter.'%');

LIKEcompares strings to match the given string. The percent sign (%) is a wildcard.

LIKE比较字符串以匹配给定的字符串。百分号 (%) 是通配符。