gpt4 book ai didi

php - 在 Laravel 中使用 Eloquent ORM 使用 'not regexp' 执行数据库搜索

转载 作者:行者123 更新时间:2023-11-29 03:59:51 24 4
gpt4 key购买 nike

我正在处理按 slug 分类搜索,我想排除末尾包含破折号和数字的结果(例如:powerbank-1)。我的查询:

$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"
}]
}

最佳答案

找到了为什么不工作的问题:从正则表达式中删除斜杠,所以它会是:

->where('slug', 'not regexp', "$search_term-[0-9]")

关于php - 在 Laravel 中使用 Eloquent ORM 使用 'not regexp' 执行数据库搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39807791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com