2016-10-01 121 views
1

我正在處理通過slug搜索類別,我想排除結尾包含破折號&(如:powerbank-1)的結果。 我的查詢:在Laravel中使用Eloquent ORM來使用'not regexp'執行數據庫搜索

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

回答

5

發現,爲什麼不工作的問題:從正則表達式刪除斜線,所以ITT將成爲:

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