I’m encountering an issue with Laravel routing in my application. Specifically, I’m getting the following error:
Internal Server Error
IlluminateRoutingExceptionsUrlGenerationException
Missing required parameter for [Route: edit_mahasiswa] [URI: mahasiswa/edit/{id}] [Missing parameter: id].
I have a route defined for editing a Mahasiswa entity:
route::group(['middleware' => ['auth:user']], function () {
Route::get('/mahasiswa/edit/{id}', [MahasiswaController::class, 'edit'])->name('edit_mahasiswa');
}
This is my Controller
public function edit($id) {
//dd($id);
$mahasiswa = Mahasiswa::find($id);
if ($mahasiswa) {
return view('mahasiswa.edit', compact('mahasiswa'));
} else {
Alert::error('Mahasiswa tidak ditemukan');
return redirect()->route('daftar_mahasiswa')->with('error', 'Mahasiswa tidak ditemukan');
}
}
In my view where I list Mahasiswa entities, I am trying to generate a URL for editing an entity:
@foreach ($mahasiswa as $m)
<tr>
<th scope="row">{{ $m->id }}</th>
<td>{{ $m->nim }}</td>
<td>{{ $m->name }}</td>
<td>{{ $m->email }}</td>
<td>
<a href="{{ route('edit_mahasiswa', ['id' => $m->id]) }}">
<button class="btn btn-warning">
<i class="ri-edit-2-fill"></i>
</button>
</a>
</td>
</tr>
@endforeach
However, I am getting an error that says:
Missing required parameter for [Route: edit_mahasiswa] [URI: mahasiswa/edit/{id}] [Missing parameter: id].
What I Have Tried:
- Verified that the route is correctly defined in web.php.
- Ensured that the id parameter is present and not null in the view.
- Added debugging information in the edit method of the controller to check if the parameter is being received.
- Cleared route and config caches using php artisan route:clear, php artisan config:clear, etc.
- Checked file permissions and .htaccess settings.
- Used url() and action() functions to generate URLs but encountered the same issue.
2
Answers
php artisan route:list yaptığında route tanımlanmış olarak gözüküyormu
The issue is probably because the
$m->id
returns innull
and it is passingnull
value to a required parameter. You can verify it by just passingnull
statically and passing an integer value.You can fix it by making sure that
$m->id
is nevernull
or make the parameter optional with{id?}
.But then make sure to handle the null case in your controller.