2016-10-29 5 views
4

私は検索フィルターを使っています。私は3つの入力 "自治体"、 "カテゴリ"、 "キーワード"を持っています。このように:Laravel 5.2 - 配列を持つフィルター

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(); 
} 

しかし、それは十分にフィルタリングされていない、このようなDD($フィルタ)リターン: enter image description here

は多分アレイの構造がOKではない、私はこのようにも試してみました: laravel 5.2 search query しかし、それは動作しません。 DD WITHOUT ($フィルター)私はこのエラーがあります:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '. 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`. . . is null and 'municipality_id', '=', 1 is null))

はあなたの助けをありがとう!

あなたがチェーン query builderインスタンス内 where()機能をできるよう
+1

よく質問を書くことを検討してください。そんなにたくさんあります。 「入力」のように、物事が空であるかどうかをチェックする。 '$ filter = [..];あなたがちょうど' $ filter = [..]; $ posts = Post :: where($ filter) - > get(); 'そうだとエラー。私は間違って何をしていますか?あなたはそのような答えをもっと得るでしょう。 –

答えて

1

。そのようなモデルでは節が連鎖メソッドにパラメータとして送信されるべき場所のため

オプション(NOT配列値):

public function search(Request $request) 
    { 
     $current = Carbon::now(); 
     $current = new Carbon(); 
     $termn = $request->input('keyword'); 
     $input_category = $request->input('category'); 
     $input_municipality = $request->input('municipality_id'); 


     $posts = Post::where('visible', 1)->where('expire_date', '>', $current); 

     if(!empty($termn)){ 
       $posts->where('title', 'LIKE' , '%'.$termn.'%'); 
     } 
     if(!empty($request->input('category'))){ 
       $posts->where('category_id', '=', $input_category); 
     } 
     if(!empty($request->input('municipality_id'))) { 
       $posts->where('municipality_id', '=', $input_municipality); 
     } 

     $post_results = $posts->get(); 
     dd($posts_results); 
} 
次のドキュメントを参照してください。

データベーステーブル(モデルではない)の配列としてクエリを送信することができます。

$users = DB::table('posts')->where([ 
    ['visible', '=', '1'], 
    ['expire_date', '>', $current], 
    // ... 
])->get(); 
1

:あなたはWHERE句が間違って使用している

$query = Post::where('visible', 1)->where('expire_date', '>', $current); 

if(!empty($termn)){ 
     $query->where('title', 'LIKE', '%'.$termn.'%') 
} 
if(!empty($request->input('category'))){ 
     $query->where('category_id', $input_category) 
} 
if(!empty($request->input('municipality_id'))) { 
     $query->where('municipality_id', $input_municipality) 
} 

$posts = $query->get(); 
関連する問題