Php 在Laravel中使用雄辩的ORM,使用';非regexp';
我处理的是按slug分类搜索,我想排除结尾包含破折号和数字的结果(比如:powerbank-1)。 我的问题是:Php 在Laravel中使用雄辩的ORM,使用';非regexp';,php,mysql,regex,eloquent,laravel-5.2,Php,Mysql,Regex,Eloquent,Laravel 5.2,我处理的是按slug分类搜索,我想排除结尾包含破折号和数字的结果(比如:powerbank-1)。 我的问题是: $categories = Category::where('active_products', ">", 0) ->where('slug', 'LIKE', "%$search_term%") ->where('slug', 'not regexp', "/$search_term-[0-9]/") ->get(); 我的结果仍然
$categories = Category::where('active_products', ">", 0)
->where('slug', 'LIKE', "%$search_term%")
->where('slug', 'not regexp', "/$search_term-[0-9]/")
->get();
我的结果仍然包含不需要的结果,slug中有-1和-2个结尾:
{
"query": "powerbank",
"categories": [
{
"id": 18,
"parent_id": 17,
"lft": 108,
"rgt": 109,
"depth": 1,
"name": "Powerbank",
"description": null,
"description2": null,
"products": 43,
"active_products": 38,
"created_at": "2016-08-25 20:51:42",
"updated_at": "2016-09-20 06:06:06",
"slug": "powerbank"
},
{
"id": 20,
"parent_id": 19,
"lft": 124,
"rgt": 125,
"depth": 1,
"name": "Powerbank",
"description": null,
"description2": null,
"products": 43,
"active_products": 38,
"created_at": "2016-08-25 20:51:43",
"updated_at": "2016-09-20 06:06:06",
"slug": "powerbank-1"
},
{
"id": 22,
"parent_id": 21,
"lft": 136,
"rgt": 137,
"depth": 1,
"name": "Powerbank",
"description": null,
"description2": null,
"products": 43,
"active_products": 38,
"created_at": "2016-08-25 20:51:43",
"updated_at": "2016-09-20 06:06:06",
"slug": "powerbank-2"
}]
}
发现问题原因:从正则表达式中删除斜杠,因此itt将:
->其中('slug','not regexp',“$search\u term-[0-9]”
找到了问题原因:从正则表达式中删除斜杠,因此itt将:
->其中('slug','not regexp',“$search\u term-[0-9]”