【发布时间】:2019-11-16 20:16:38
【问题描述】:
我正在尝试根据查询字符串过滤产品。我的目标是从集合中获取产品(如果已提供),否则获取所有产品。有人可以帮我看看下面的代码有什么问题吗?
$products = \App\Product::where([
'collection' => (request()->has('collection')) ? request('collection') : '[a-z]+',
'type' => (request()->has('type')) ? request('type') : '[a-z]+'
])->get();
PS.:我也尝试过 'regex:/[a-z]+',它不起作用...
$products = \App\Product::where(['collection' => (request()->has('collection')) ? request('collection') : 'regex:/[a-z]+'])->get();
【问题讨论】:
-
where 功能使用列、运算符和值...这些值是文字值,而不是 SQL 表达式、函数、列等
-
除了@lagbox 评论,试试
DB::raw('[a-z]+')。 -
如果这是使用 MySQL,您也许可以执行
where('collection', 'rlike', '[a-z]+')或类似的操作
标签: php regex laravel laravel-5