2016-07-05 5 views
0

はイム()orWhereを使用しているので、私は全く正しいが、私は考えていない私の結果、いくつかの事が間違ってい私のLaravel 5.2アプリlaravelマルチフィールド検索フォーム

price 
rooms 
type 
rent 

にマルチフィールド検索フォームを持っています。私は、入力フィールド間にANDが必要です。私はこのための任意の解決策が欲しい

public function search() 
    { 

     $un_price = \Request::get('un_price'); 
     $un_rooms = \Request::get('un_rooms'); 
     $un_type = \Request::get('un_type'); 
     $un_rent = \Request::get('un_rent'); 
     $units = DB::table('units')->whereIn('un_status', [1]) 
      ->where('un_price','like','%'.$un_price.'%') 
      ->orWhere("un_rooms", "LIKE", "%$un_rooms%") 
      ->orWhere("un_type", "LIKE", "%$un_type%") 
      ->orWhere("un_rent", "LIKE", "%$un_rent%") 
      ->paginate(20); 
     return view('home.units.show', compact('units')); 
} 
+0

代わりにorWhereの代わりに使用しますか? –

+0

私が何も得られない場所を使用する場合 – fido

+0

%markのorathereフィールドでconcat変数を使用してください。そうでなければ "%$ un_rooms% 'のようなun_roomsを探しています..." –

答えて

2

クエリ文字列フィルタリングを使用します。

public function search(Request $request) 
{ 
    $unit = (new Unit)->newQuery(); //where Unit is the model 
    if($request->has('un_price'){ 
     $unit->where('un_price',$request->un_price) 
    } 
    if($request->has('un_rooms'){ 
     $unit->where('un_rooms',$request->un_rooms) 
    } 
    //go on until the end 
    $units = $unit->get(); 
    return view('home.units.show', compact('units')); 
} 

このようにして、受け取った入力に応じて必要なすべての結果を得ることができます。

+0

ありがとうございました – fido

+0

このケースでは、un_priceの間を検索して2つの入力フィールドを追加する方法を教えてください。un_price_fromとun_price_to – fido

+1

elseif($ request-> has( 'un_price_from'、 'un_price_to')){ $ unit-> whereBetween( 'un_price'、[$ request - > un_price_from、$ request-> un_price_to]); } – fido