I get the error:
Missing required parameter for [Route: postDetails] [URI: {language}/cars/{id}] [Missing parameter: id]."
My Routes:
Route::prefix('{language}')->group(function () {
Route::get('/',[HomeController::class,'homePage'])->name('home');
Route::get('cars',[AllPostsController::class,'allPosts'])->name('allPosts');
Route::get('cars/{id}',[AllPostsController::class,'details'])->name('postDetails');
});
My Middleware:
public function handle(Request $request, Closure $next): Response
{
App::setLocale($request->language);
return $next($request);
}
My Controller:
public function details($language,$id){
$post = Car::find($id);
if($post) {
$license_no = $post->license_no;
if ($license_no) {
$prefix = substr($license_no, 0, 4);
$suffix = str_repeat('*', strlen($license_no) - 4);
$short_license_no = $prefix . $suffix;
$post->short_license_no = $short_license_no;
}
$data = Car::select('m.name as manufacturer_name','mo.name as model_name')
->leftJoin('manufacturers as m','m.id','cars.manufacturer_id')
->leftJoin('models as mo','mo.id','cars.model_id')
->where('cars.id',$id)
->first();
return view('public.carDetail',compact('post','data','language'));
}else {
abort(404);
}
}
My Link:
<a href="{{ route('postDetails', ['language' => app()->getLocale(), 'id' => $post->id]) }}" class="stretched-link"></a>
2
Answers
Make sure you are not passing
null
in yourid
param at link. debug it usingdd($post->id)
before your anchor tag(link). Probably$post
is getting as null. Apart from that everything looks correct.You can try edit
prefix(‘{language’}) ->
group([‘prefix’ => ‘{language}’])
It will work fine