drwf69817
2016-10-01 14:30
浏览 390
已采纳

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

I'm dealing with searching in categories by slug and I want to exclude results which contain dash & number at the end (like: powerbank-1). My query :

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

My result still contain unwanted results with -1 & -2 endings in slug:

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

图片转代码服务由CSDN问答提供 功能建议

我正在处理按类别搜索类别,我想要排除包含破折号的结果 最后的数字(如:powerbank-1)。 我的查询:</ p>

  $ categories = Category :: where('active_products',“&gt;”,0)\  n  - &gt; where('slug','LIKE',“%$ search_term%”)
  - &gt; where('slug','not regexp',“/ $ search_term- [0-9] /”)  
  - &gt; get(); 
 </ code> </ pre> 
 
 

我的结果仍包含不需要的结果-1和&amp; slu in中的-2结尾:</ p>

  {
“查询”:“powerbank”,
“类别”:[
 {
“id”:18,\  n“parent_id”:17,
“lft”:108,
“rgt”:109,
“”深度“:1,
”名称“:”Powerbank“,
”描述“:null,\  n“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,
“”深度“:1,
”名称“:”Powerbank“,
”描述“: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“:”2  016-08-25 20:51:43“,
”updated_at“:”2016-09-20 06:06:06“,
”slug“:”powerbank-2“
}] 
} \  n </ code> </ pre> 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题