繁体   English   中英

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

[英]Using Eloquent ORM in Laravel to perform search of database using 'not regexp'

我正在处理按段搜索类别,并且要排除最后包含破折号和数字的结果(例如:powerbank-1)。 我的查询:

$categories = Category::where('active_products', ">", 0)
    ->where('slug', 'LIKE', "%$search_term%")
    ->where('slug', 'not regexp', "/$search_term-[0-9]/")
    ->get();

我的结果仍包含-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]")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM