【发布时间】:2017-03-12 03:31:22
【问题描述】:
我正在做一个搜索过滤器,我有 3 个输入“市政”、“类别”、“关键字”,我正在尝试将值插入到数组中,如果输入不为空。像这样:
public function search(Request $request)
{
$filter = array(["'visible', '=' , 1"],["'expire_date', '>', $current"]);
if(!empty($termn)){
$filter[] =["'title', 'LIKE' , '%'.$termn.'%'"];
}
if(!empty($request->input('category'))){
$filter[] = ["'category_id', '=', $input_category"];
}
if(!empty($request->input('municipality_id'))) {
$filter[] = ["'municipality_id', '=', $input_municipality"];
}
dd($filter);
$posts = Post::where($filter)->get();
}
也许数组的结构不对,我也试过这样: laravel 5.2 search query 但它不起作用。 没有 dd($filter) 我有这个错误:
SQLSTATE[42000]:语法错误或访问冲突:1064 你有一个 SQL 语法错误;检查与您对应的手册 MariaDB 服务器版本,用于在 '.
is null and `'municipality_id', '=', 1` is null)' at line 1 (SQL: select * from `posts` where (`'visible', '=' , 1` is null and `'expire_date', '>', 2016-10-29 13:29:30` is null and `'category_id', '=', Scegli una categoria`...附近使用的正确语法为 null,'municipality_id', '=', 1为 null))
感谢您的帮助!
【问题讨论】:
-
考虑更好地编写您的问题。那里有这么多。像“输入”和检查东西是否为空。你刚才说
$filter = [..]; $posts = Post::where($filter)->get();给了某某错误不是更好吗。我究竟做错了什么?这样你会得到更多的答案。