Laravel 5.6 - 带有2个变量的request()

时间:2018-06-07 13:58:44

标签: laravel-5 request

我通过request() - >变量过滤结果。当只有一个变量时,视图会返回,例如...... / products / summer,只显示属于特定季节的产品。

我想要做的是调用第二个变量/ products / summer?product_category =衬衫显示上面的季节产品,同时过滤属于特定类别的那些(例如.shirts)。

这是我的控制器部分:

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,



      ]);

有没有办法在上面的elseif中包含season和product_category,以便它对应于summer?product_category = shirts?

我的导航链接传递了这个:

href="{{ route ('season.index',['season' => $season->slug, 'product_category' => $product_category->slug])}}"

路线是:

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

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

我完全不明白你需要什么 如果这段代码没用,请多解释一下,然后我改变它:

我的观点:

<a
href="{{ route ('season.index',['season' =>$season->slug, 'product_category' => $product_category->slug])}}"
>send</a>

我的路线:

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

SeasonController控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SeasonController extends Controller
{
    public function index(Request $request)
    {
        if ($request->get('season'))
        {
            # do something

        }elseif($request->get('product_category'))
        {
            # do something
        }
        else
        {
            # do something
        }
    }
}