Diego Utreras
Diego Utreras

Reputation: 111

The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE. in Laravel 8

I'm getting this error when I'm trying to register something from a form. I'm going to post the code and after that explain the situation

View Code

@extends('layouts.app')

@section('content')
<div class="container">

    <h1>Crear Especialidad</h1>
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Register') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('especialidades.create') }}">
                        @csrf
                        <div class="form-group row">
                            <label for="nombre" class="col-md-4 col-form-label text-md-right">{{ __('Nombre') }}</label>
                            
                            <div class="col-md-6">
                                <input id="nombre" type="text" class="form-control @error('nombre') is-invalid @enderror" name="nombre" value="{{ old('nombre') }}" required autocomplete="nombre" autofocus>

                                @error('nombre')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        
                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Registrar') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Controller code

 public function create(array $data)
    {
        return Especialidades::create($data);  
    }

Routes

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/nuevaEspecialidad', [EspecialidadesController::class,'nuevo'])->name('nuevaEspecialidad');
Route::resource('/especialidades', EspecialidadesController::class);
Route::get('/gestionarMedicos', [PersonaController::class,'mostrarMedicos'])->name('personaMostrarMedicos');

Dunno if it is a problem with overwriting or something similar but the resource route should support post for the create function. I'm trying to insert the data from the form, a name as you can see in the view to the database.
I'm new on Laravel so dunno what can be the cause. I will gladly provide you with extra code if needed or explain better the situation if it isn't explained enough.

Upvotes: 1

Views: 2996

Answers (2)

runtimeTerror
runtimeTerror

Reputation: 426

In your case, it should be {{ route('especialidades.store') }}

The reason being that default method for creating a row in db in a resource controller is App\Http\Controllers\<YourResourceControllerName>@store

/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

Here the create method is only for showing the form of the store method and not the function for storing the data itself.

Upvotes: 5

STA
STA

Reputation: 34668

You are using resource, where especialidades.create it will accept get method. Your submit method is post, so you need to change :

{{ route('especialidades.store') }}

Upvotes: 4

Related Questions