2017-01-18 9 views
0

現時点では、IAMは実際にLaravelのフィルタリングとページ設定を苦労しています。 http://snkrgllry.herokuapp.com/では、フィルタモーダルで画像を並べ替えることができます。たとえば、昇順が好きな場合は、2番目のページに移動すると[2]というビューは見つかりません。フォームを介してパラメータを送信した後のLaravelページネーション(GET)

フォームを介してフィルタリングするためのパラメータをコントローラに送信します。このコントローラはクエリを作成し、そのデータをすべてビューに戻します。これまでのところすべてうまくいっていますが、2番目のページはまったく動作していません。

は、ここでこれは、私の悪いプログラミングの私の最初のWEB-/laravelプロジェクトを言い訳してくださいコントローラ機能であるブレード

<div class="container"> 
    <div class="col-xs-12 row-centered"> 
     {{ $images->appends(request()->input())->links()}} 
     </div> 
</div> 

に私のコードです。

public function filter(Request $request) 


{ 
    $brand = $request->brand; 
    $color = $request->color; 
    $style = $request->style; 
    $material = $request->material; 
    $year = $request->year; 
    $shape = $request->shape; 
    $sorting = $request->sort; 
    $page = $request->page; 
    $user_id = $request->user_id; 

    $sortingMethod = 'desc'; 
    $sortingParameter = 'created_At'; 

    //Abfrage wie sortiert werden soll 

    if ($sorting == 'uploadDesc') { 
     $sortingMethod = 'desc'; 
     $sortingParameter = 'created_At'; 
    } else if ($sorting == 'uploadAsc') { 
     $sortingMethod = 'asc'; 
     $sortingParameter = 'created_At'; 
    } else if ($sorting == 'leer') { 
     $sortingMethod = 'desc'; 
     $sortingParameter = 'created_At'; 
    } else if ($sorting == 'likesAsc') { 
     $sortingParameter = 'count'; 
     $sortingMethod = 'asc'; 
    } else if ($sorting == 'likesDesc') { 
     $sortingParameter = 'count'; 
     $sortingMethod = 'desc'; 
    } 

    //$imagesQuery = DB::table('images')->select('brand', 'color', 'style', 'material', 'shape', 'year', 'id', 'path', 'created_at')->where('contest', 'true'); 
    $imagesQuery = DB::table('images') 
     ->leftJoin('likes', 'images.id', '=', 'likes.image_id') 
     ->select('images.*', DB::raw("count(likes.image_id) as count")) 
     ->groupBy('images.id', 'images.brand', 'images.user_id', 'images.color', 'images.style', 'images.material', 'images.shape', 'images.year', 'images.desc', 'images.path', 'images.name', 'images.model', 'images.contest', 'images.remember_token', 'images.created_at', 'images.updated_at') 
     ->orderBy($sortingParameter, $sortingMethod); 


    $brands = DB::table('images')->select('brand')->groupBy('brand')->get(); 
    $colors = DB::table('images')->select('color')->groupBy('color')->get(); 
    $styles = DB::table('images')->select('style')->groupBy('style')->get(); 
    $materials = DB::table('images')->select('material')->groupBy('material')->get(); 
    $years = DB::table('images')->select('year')->groupBy('year')->get(); 
    $shapes = DB::table('images')->select('shape')->groupBy('shape')->get(); 


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

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

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


    if ($page == 'contest') { 
     $imagesQuery->where('images.contest', '=', 'true'); 

     $brands->where('contest', 'true'); 
     $colors->where('contest', 'true'); 
     $styles->where('contest', 'true'); 
     $materials->where('contest', 'true'); 
     $years->where('contest', 'true'); 
     $shapes->where('contest', 'true'); 
    } 

    if ($page == 'profile') { 
     $imagesQuery->where('images.user_id', '=', $user_id); 
     $user = User::find($user_id); 
    } 

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

    return view($page)->with(compact('images', 'brands', 'colors', 'styles', 'materials', 'years', 'shapes', 'user')); 

} 

そして、私は、フィルタのフォームを送信した場合、これは、呼び出されます私のルートです。

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

あなたの側からこの問題を解決する方法がありますか?私はそれについて多くを読んだが、私はまだそれを完了しませんでした。

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

敬具ラース

答えて

0

あなたは、パラメータとしてページを渡す:

return view($page) 

だから、それは2ページ目の '2' です。ページ番号ではなく、テンプレート名を渡すだけです。渡された別の名前は pageで、ページネーションに使用されます。

0

ニースシューズ! :)

問題はフロントエンドコードにあります。 <a href="http://snkrgllry.herokuapp.com/indexFilter?_token=m2L6LXgKELcc66JsDgxFQaF6WxhfXfep22LC0PXk&amp;brand=leer&amp;color=leer&amp;style=leer&amp;material=leer&amp;shape=leer&amp;year=leer&amp;sort=likesAsc&amp;page=2">2</a>

0

おかげで、私はそれが(明日までプロジェクトの貢献度をそれを解決するために管理していないだろうたくさん:あなたは次のページをクリックすると、pageの要求paramがここにHTMLコードがあります代わりにindex

2です私の大学の教授)、今私は自分の間違いを知っているので、私はそれが私のためにうまくいくように "修正"しました。あなたの解決策で問題を解決するために最善を尽くします!

関連する問題