2017-01-13 8 views
1

というフォームの結果からクエリの依存関係を変更します。Laravel - LaravelのSQLクエリで少し苦労している瞬間、

私はgetによってイメージコントローラに送信する6個のドロップダウンを持つフォームを持っています。コントローラに送信する内容に依存して、クエリが変更されるはずです。

aが "空"の場合(value = "leer")、これはクエリから除外する必要があります。

これは私のhtmlです:、私はルート

Route::get('/filter', '[email protected]'); 

経由でこのフォームを送信すると、これは私のコントローラ機能である

<form action="/filter" method="get"> 

               {{csrf_field()}} 
               <div class="form-group"> 
                <label for="brand">Brand</label> 
                <select class="form-control" id="brand" name="brand"> 
                 <option value="leer"></option> 
                 @foreach($brands as $brand) 

                  <option value="{{$brand->brand}}">{{$brand->brand}}</option> 


                 @endforeach 


                </select> 
               </div> 
               <div class="form-group"> 
                <label for="color">Color</label> 
                <select class="form-control" id="color" name="color"> 
                 <option value="leer"></option> 

                 @foreach($colors as $color) 

                  <option value="{{$color->color}}">{{$color->color}}</option> 


                 @endforeach 
                </select> 
               </div> 
               <div class="form-group"> 
                <label for="style">Style</label> 
                <select class="form-control" id="style" name="style"> 
                 <option value="leer"></option> 
                 @foreach($styles as $style) 

                  <option value="{{$style->style}}">{{$style->style}}</option> 


                 @endforeach 
                </select> 
               </div> 
               <div class="form-group"> 
                <label for="material">Material</label> 
                <select class="form-control" id="material" name="material"> 
                 <option value="leer"></option> 
                 @foreach($materials as $material) 

                  <option value="{{$material->material}}">{{$material->material}}</option> 


                 @endforeach 
                </select> 
               </div> 
               <div class="form-group"> 
                <label for="shape">Shape</label> 
                <select class="form-control" id="shape" name="shape"> 
                 <option value="leer"></option> 
                 @foreach($shapes as $shape) 

                  <option value="{{$shape->shape}}">{{$shape->shape}}</option> 


                 @endforeach 
                </select> 
               </div> 
               <div class="form-group"> 
                <label for="year">Year</label> 
                <select class="form-control" id="year" name="year"> 
                 <option value="leer"></option> 
                 @foreach($years as $year) 

                  <option value="{{$year->year}}">{{$year->year}}</option> 


                 @endforeach 

                </select> 
               </div> 

               <input type="submit" class="btn" value="Filter"> 



               <button type="button" class="btn" data-dismiss="modal">Close 
               </button> 

              </form> 

句は何もDEFAULTIFないことで、空のあるべき場所に選ばれます形。たとえば、ブランドのドロップダウンやカラードロップダウンから選択すると、クエリは([[brand '、$ brand]、[' color '、$ color]]) - > paginate(12)のようになります。

public function filter(Request $request){ 

    $brand = $request->brand; 
    $color = $request->color; 
    $style = $request->style; 
    $material = $request->material; 
    $year = $request->year; 
    $shape = $request->shape; 

    $images = DB::table('images')->select('brand', 'color', 'style', 'material', 'shape', 'year', 'id', 'path', 'created_at')->where('year',$year)->paginate(12); 

    if ($brand == 'leer') { 
     $images->where('brand', '=', $brand); 
    } 

    if ($color == 'leer') { 
     $images->where('color', '=', $color); 
    } 

    if ($style == 'leer') { 
     $images->where('style', '=', $style); 
    } 

    if ($material == 'leer') { 
     $images->where('material', '=', $material); 
    } 

    if ($shape == 'leer') { 
     $images->where('shape', '=', $shape); 
    } 

    if ($year == 'leer') { 
     $images->where('year', '=', $year); 
    } 




    return view('index')->with(compact('images')); 

時には動作しますが、私はその事故によって、明らかに動作しません。

本当にありがとうございます。

Larsに事前に感謝します。

答えて

1

where節の後にpaginateを呼び出す必要があります。 builderオブジェクトのページネーションは、LengthAwarePaginatorオブジェクトを生成します。 Aこの呼び出し元の呼び出しによって、(マジックメソッド__callを使用して)期待される応答ではないページネーションコレクションの場所が得られます。

試してみてください。

$imagesQuery = DB::table('images')->select('brand', 'color', 'style', 'material', 'shape', 'year', 'id', 'path', 'created_at')->where('year',$year); 

     if ($brand == 'leer') { 
      $imagesQuery->where('brand', '=', $brand); 
     } 

     if ($color == 'leer') { 
      $imagesQuery->where('color', '=', $color); 
     } 

     if ($style == 'leer') { 
      $imagesQuery->where('style', '=', $style); 
     } 

     if ($material == 'leer') { 
      $imagesQuery->where('material', '=', $material); 
     } 

     if ($shape == 'leer') { 
      $imagesQuery->where('shape', '=', $shape); 
     } 

     if ($year == 'leer') { 
      $imagesQuery->where('year', '=', $year); 
     } 

$images = $imagesQuery->paginate(12); 

return view('index')->with(compact('images'));