skip to Main Content

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:

  1. Verified that the route is correctly defined in web.php.
  2. Ensured that the id parameter is present and not null in the view.
  3. Added debugging information in the edit method of the controller to check if the parameter is being received.
  4. Cleared route and config caches using php artisan route:clear, php artisan config:clear, etc.
  5. Checked file permissions and .htaccess settings.
  6. Used url() and action() functions to generate URLs but encountered the same issue.

2

Answers


  1. php artisan route:list yaptığında route tanımlanmış olarak gözüküyormu

    Login or Signup to reply.
  2. The issue is probably because the $m->id returns in null and it is passing null value to a required parameter. You can verify it by just passing null statically and passing an integer value.

    <a href="{{ route('edit_mahasiswa', ['id' => null]) }}">Button</a> //for error
    <a href="{{ route('edit_mahasiswa', ['id' => 1]) }}">Button</a> // for correct results
    

    You can fix it by making sure that $m->id is never null or make the parameter optional with {id?}.

    Route::get('/mahasiswa/edit/{id?}', [MahasiswaController::class, 'edit'])-`>name('edit_mahasiswa');`
    

    But then make sure to handle the null case in your controller.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search