【问题标题】:Using Eloquent ORM in Laravel to perform search of database using 'not regexp'在 Laravel 中使用 Eloquent ORM 使用“非正则表达式”执行数据库搜索
【发布时间】:2016-10-01 14:30:56
【问题描述】:

我正在按 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"
  }]
}

【问题讨论】:

    标签: php mysql regex eloquent laravel-5.2


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-11-03
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 2014-10-21
      • 2020-10-04
      • 1970-01-01
      相关资源
      最近更新 更多