Ad

Not Getting Id From URL In Laravel 6

- 1 answer

I am very new in laravel and trying to get id from URL example.com/cmsedit/3 but failing.

My routes:-

Route::get('cmsedit/{id}', function($id)
{
    return view('admin.cmsed');
});

when I am trying to get an ID in my view file

<?PHP echo $id ?>

It says Undefined variable: id and if I am using $request->id it is also throwing an error Undefined variable: request

Laravel version = 6

Ad

Answer

You should return the given ID from Router's callback:

Route::get('cmsedit/{id}', function($id) {
    return view('admin.cmsed', ['id' => $id]);
});

or,

Route::get('cmsedit/{id}', function($id) {
    return view('admin.cmsed', compact('id'));
});
Ad
source: stackoverflow.com
Ad