Home > Blockchain >  Missing required parameter for [Route: building.floors] [URI: building/{building}/floors]
Missing required parameter for [Route: building.floors] [URI: building/{building}/floors]

Time:08-04

I need your help. How can I write in the following href using route?

I have the following route:

Route::get('/building/{building}/floors', [BuildingController::class, 'floors'])->name('building.floors');

In the view I have:

<td><a href="{{URL::to('/building/' . $building->id . '/floors') }}" >{{ $building->floors }}</a></td>

I tried to write it in the following way

<td><a href="{{route('building.floors' , ['id' => $building->id])}}" >{{ $building->floors }}</a></td>

but I get the error Missing required parameter for [Route: building.floors] [URI: building/{building}/floors].

Can anyone kindly help me?

CodePudding user response:

Just a guess but should ['id' => $building->id] be ['building' => $building->id] (because building is the name of the variable in the route)?

CodePudding user response:

@Sarah you need to understand the error.

Always remember this Route::get('/building/{building}/floors', [BuildingController::class, 'floors'])->name('building.floors'); here {building} is your dynamic parameter name, thus you need to pass the same parameter {building} in your blade anywhere you use this route.

we will use id if the route is Route::get('/building/{id}/floors', [BuildingController::class, 'floors'])->name('building.floors'); because we are passing {id} as a dynamic parameter.

In this code /building/{building}/floors if your parameter buidling is representing building id than you must use a proper and meaningfull parameter name like {building_id} in route file and pass ['building_id' => $building->id] as building represent your class/object name and you are trying to pass it's property id as a parameter. This help to avoid confusions also other developer find it easy to read and work on the same.

  • Related