2017-08-07 1 views
-3

私のアプリケーションで価格フィルタを作成するにはどうすればいいですか?価格でラベールベースのフィルタ

public function indexproduct(Request $request) { 
     $query = Product::orderBy('created_at','desc'); 
     if($request->keyword){ 
      // This will only executed if you received any keyword 
      $query = $query->where('title','like','%'.$keyword.'%'); 
     } 
     if($request->min_price && $request->max_price){ 
      // This will only executed if you received any price 
      // Make you you validated the min and max price properly 
      $query = $query->where('price','>=',$request->min_price); 
      $query = $query->where('price','<=',$request->max_price); 
     } 
     $products = $query->paginate(6); 
     return view('frontend.shop', compact('products')); 
    } 

ビュー:

<form class="form-inline" action="{{route('shop')}}" method="GET"> 
      Min <input class="form-control" type="text" name="min_price"> 
      Max <input class="form-control" type="text" name="max_price"> 
      Keyword <input class="form-control" type="text" name="keyword" > 
      <input class="btn btn-default" type="submit" value="Filter"> 
     </form> 

私の製品が表示されます、私は

laravel 5.4を使用してい

は、私は現在、このようなものを持っています。私はまた、ユーザーが結果をフィルタリングできるように、私の製品の上にフィルタ選択ボックスを追加したい。

更新:ページはロードされますが、結果は表示されません。ちょうどリフレッシュするだけです。 ページ!

database

+0

あなたは何を試しましたか?どんな変数を渡していますか? –

+0

現在はありません!私は方法を知らない原因! –

+0

私の答えをチェックしてください。モデル、テーブル名、質問に関連するビューなどの情報を提供していただければ幸いです。 –

答えて

1

まず第一に、あなたはすべてのあなたのフィルタを置くことができるフォームを作成する必要があります。

<form action="{{ url('url/to/your/indexproduct')}}" method="GET"> 
    <input type="text" name="min_price"> //User can input minimum price here 
    <input type="text" name="max_price"> //User can input maximun price here 
    <input type="text" name="keyword" > // User can input name or description 
    <input type="submit" value="Filter"> 
</form> 

その後、送信ボタンをクリックすると、コントローラはそのフォームからデータを受信します。あなたがそれをチェックしたいならば。これは好きですか?あなたのデータはあなたのコントローラに送信されたことをしているとき

public function indexproduct(Illuminate\Http\Request $request) { 
    dd($request->all()) // This will show all your filters 
} 

その後、あなたはあなたのクエリを修正し、あなたのフィルタを含める必要があります。 (これほど多くの方法がありますが、これは単純なものです)

public function indexproduct(Illuminate\Http\Request $request) { 
    $query = Product::orderBy('created_at','desc'); 
    if($request->keyword){ 
     // This will only execute if you received any keyword 
     $query = $query->where('name','like','%'.$keyword.'%'); 
    } 
    if($request->min_price && $request->max_price){ 
     // This will only execute if you received any price 
     // Make you you validated the min and max price properly 
     $query = $query->where('price','>=',$request->min_price); 
     $query = $query->where('price','<=',$request->max_price); 
    } 
    $products = $query->paginate(5); 
    return view('frontend.shop', compact('products')); 
} 

私はあなたを助けてくれることを願っています。

+0

ok broこのエラーが発生しました。メソッドリンクが存在しません。 –

+0

私は自分の答えを改訂しました。ページネーションを忘れました。 '$ products = $ query-> paginate(5);'。あなたはエラーがページネーションかもしれません。 –

+0

はページをロードしますが、結果は表示されません。ページをリフレッシュするだけです。 –