路由获取通过 Laravel 5.2 中的 href 传递的 GET 值

Route getting GET value passed via a href in Laravel 5.2

亲爱的这里所有的好人,

我正在使用 Laravel 5.2 制作一个电子商务平台(对它来说非常新),我想制作一个锚标记,当您单击它时,它会将 GET 值传递给路由。它实际上转到下面的 link http://localhost:8000/products/803160151?product_choice_id=1 但用 404 页面代替。估计是我路由函数写错了?请在下面找到我的代码供您参考,如果您需要更多信息,请告诉我。谢谢

routes.php

Route::get('products/{product_code}?product_choice_id={product_choice_id}', function($product_code, $product_choice_id){
      $product = Product::where('product_code',$product_code)->firstOrFail();
    $product_choice = Product_Choice::where('id', $product_choice_id)->get();
    $reviews = Review::where('product_id', $product->id)->get();
    $stars = Review::where('product_id', $product->id)->pluck('stars');    
    if(count($stars)){
        $total_stars = collect($stars)->sum()/count($stars);
        }   else {
            $total_stars = "not rated yet";
        };
    return view('products.details')
        ->with('product', $product)
        ->with('product_choice',$product_choice)
        ->with('reviews',$reviews)
        ->with('stars',$stars)
        ->with('total_stars',$total_stars);
});

interface.blade.php

    @php
      $product_choice = App\Product_Choice::where('product_id',$product->id)->where('default',1)->firstOrFail()
    @endphp

<a class="aa-product-img" href="{{ url('/products').'/'.$product->product_code.'?product_choice_id='.$product_choice->id }}">

您不需要定义参数。只需定义路线

Route::get('prdoucts/{product_code}', function(Request $request) {
    $product_choice_id = $request->input('product_choice_id');
});

别忘了补充

use Illuminate\Http\Request;

在请求中,您将找到所有 GET 参数。这也适用于 post / put / patch / delete - 当然你需要定义使用这些方法的路由