Search code examples
phplaravellaravel-routing

Laravel error: Missing required parameters for Route Error


I keep getting this error

Illuminate  \  Routing  \  Exceptions  \  UrlGenerationException PHP 8.1.1 9.1.0 Missing required parameter for [Route: client.brand_clicked] [URI: brands/{brand_id}] [Missing parameter: brand_id].

When ever i click my products site and i had this error, because of the brand_click sidebar filter button

This is my sidebar has route:client.brand_clicked

<div class="h4 col-xs-b25" id="check-brand">Thương Hiệu</div>
    <label class="checkbox-entry">
        @foreach($brands as $brand)
            <a href="{{ route('client.brand_clicked',['brand_id'=>$brand->ma_TH])}}">
                <button class="btn" style="width:98px; margin-bottom:5px;" value = "{{ $brand->ma_TH}}"  name="btn-brand">{{ $brand->ten_TH}}</button>
            </a>
        @endforeach
    </label>
    <div class="empty-space col-xs-b10"></div>

This is my Controller

public function brand_clicked($brand_id){
    $categories = Category::where('parent_id',0)->get();
    $products = Product::where('brand_id',$brand_id)->paginate(9);
    $brands = DB::table('product')->leftJoin('brands','product.brand_id','=','brands.brand_id')->select('brands.brand_id','brands.brand_name')->groupBy('brands.brand_id','brands.brand_name')->get();
    $total = count($products);
    return view('client.products', compact('categories','brands','products','total'));
}

This is my Route - web.php file

Route::get('/brands/{brand_id}',[App\Http\Controllers\client\ProductController::class,'brand_clicked'])->name('client.brand_clicked');

Solution

  • My guess is that problem is this line:

    <a href="{{ route('client.brand_clicked',['brand_id'=>$brand->ma_TH])}}">
    

    ad least one $brand object does not have "ma_TH" attribute. $brand->ma_TH is null or empty.