Skip to content
Advertisement

The PUT method is not supported for this route. Supported methods: GET, HEAD. (Using resource)

I have saw this issue couple of times but not in this exact form.

Here’s my: edit.blade.php

<form action="{{ route('admin.korisnici.update', ['korisnik' => $korisnik->id]) }}" method="POST">
    {{csrf_field()}}
    {{method_field('PUT')}}
    @foreach($uloge as $uloga)
        <div class="form-check">
            <input type="checkbox" name="uloge[]" value="{{ $uloga->id }}" 
            {{ $korisnik->hasRole($uloga->naziv_tipa)?'checked':'' }}>
            <label>{{ $uloga->naziv_tipa }}</label>
        </div>
    @endforeach
    <button type="submit" class="btn btn-primary btn-sm">Ažuriraj</button>
</form>

My controller: KorisniciController.php

public function update(Request $request, $id)
{
        if (Auth::user()->id == $id) {
            return redirect()->route('admin.korisnici.index');
        }

        $korisnik = User::find($id);
        $korisnik->roles()->sync($request->uloge);

    return redirect()->route('admin.korisnici.index');
}

And my web.app

Route::namespace('Admin')->prefix('admin')->middleware(['auth', 'roles.admin'])->name('admin.')->group(function(){
    Route::resource('/korisnici', 'KorisniciController')->except(['show', 'create', 'store']);
});

php artisan route:list returns

+--------+-----------+----------------------------------+-------------------------+------------------------------------------------------------------------+--------------------------+
| Domain | Method    | URI                              | Name                    | Action                                                                 | Middleware               |
+--------+-----------+----------------------------------+-------------------------+------------------------------------------------------------------------+--------------------------+
|        | GET|HEAD  | /                                |                         | Closure                                                                | web                      |
|        | GET|HEAD  | admin                            |                         | Closure                                                                | web,auth,roles.admin     |
|        | GET|HEAD  | admin/korisnici                  | admin.korisnici.index   | AppHttpControllersAdminKorisniciController@index                   | web,auth,roles.admin     |
|        | DELETE    | admin/korisnici/{korisnici}      | admin.korisnici.destroy | AppHttpControllersAdminKorisniciController@destroy                 | web,auth,roles.admin     |
|        | PUT|PATCH | admin/korisnici/{korisnici}      | admin.korisnici.update  | AppHttpControllersAdminKorisniciController@update                  | web,auth,roles.admin     |
|        | GET|HEAD  | admin/korisnici/{korisnici}/edit | admin.korisnici.edit    | AppHttpControllersAdminKorisniciController@edit                    | web,auth,roles.admin     |

Advertisement

Answer

The route is expecting parameter korisnici, but you pass korisnik

Actually, you don’t need to specify the key at all, just pass the model to the route

<form action="{{ route('admin.korisnici.update', $korisnik) }}" 
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement