jeudi 7 juin 2018

Laravel 5.6 - request() with 2 variables

I am filtering results via request()->variable. When there is only one variable the view comes back as it should eg.../products/summer, displaying only products that belong to a certain season.

What i'm trying to do is call in a second variable /products/summer?product_category=shirts displaying the season products as above while filtering those that belong to a specific category (eg.shirts).

This is my controller part:

public function index()
    {
      if (request()->season) {
        $products = Product::with('seasons')->whereHas('seasons', function ($query){
        $query->where('slug', request()->season);
      })->get();
        $product_categories = ProductCategory::with('seasons')->whereHas('seasons', function ($query){
        $query->where('slug', request()->season);
      })->get();
        $season = Season::where('slug', request()->season)->firstOrFail();



      } **elseif (request()->product_category)** {
        $products = Product::with('product_categories')->whereHas('product_categories', function ($query){
        $query->where('slug', request()->product_category);
      })->get();
        $product_categories = ProductCategory::with('seasons')->get();
        $season = Season::where('slug', request()->season)->first();

}......

return view('season')->with([
        'products' => $products,
        'product_categories' => $product_categories,
        'season' => $season,



      ]);

Is there a way to include season and product_category in the elseif above so that it corresponds to summer?product_category=shirts ?

My nav link passes this:

href=""

The route is:

Route::get('/products/{season?}', 'SeasonController@index')->name('season.index');

Thank you for your time.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire