Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 过滤前缀时的Laravel sql查询_Php_Sql_Laravel - Fatal编程技术网

Php 过滤前缀时的Laravel sql查询

Php 过滤前缀时的Laravel sql查询,php,sql,laravel,Php,Sql,Laravel,我有一个名为MyModel的模型,连接到表my_models,其中一列是“unified_code”。统一列存储varchar,例如“500230338383”。统一数据的长度通常为12。我想从我的模型中提取前缀为“5002”的所有集合,请问如何使用laravel实现这一点 我知道下面的代码将返回空响应。什么是正确的方法 $collection = MyModel::where('unified_code', "5002")->get(); 这应该行得通 $colle

我有一个名为MyModel的模型,连接到表my_models,其中一列是“unified_code”。统一列存储varchar,例如“500230338383”。统一数据的长度通常为12。我想从我的模型中提取前缀为“5002”的所有集合,请问如何使用laravel实现这一点

我知道下面的代码将返回空响应。什么是正确的方法

$collection = MyModel::where('unified_code', "5002")->get();
这应该行得通

$collection = MyModel::where('unified_code', 'like', '5002%')->get();

//with a variable
$collection = MyModel::where('name','like','%'.$variable.'%')->get();


文档:

您需要像使用通配符一样使用

$collection = MyModel::where('unified_code', 'LIKE',  "5002%")->get();

这里需要注意的是,通配符匹配该侧的任何内容。因此,如果希望字符串以
5002
开头,则只需在右侧放置一个通配符。如果希望所有字符串都包含
5002
,则应在两侧放置通配符。

$collection=MyModel::where('unified_-code','LIKE',“%”$query。“%”->get()