2 votes

The POST method is not supported.. ROUTE::resources

Hello, I have a problem with the routes in laravel:

in my web.php file where the routes go I have the following route:

Route::resources(['tickets' => 'TicketController']);

I have several views generated, one as an index of the tickets, another to update and another to create a ticket. My problem is that when I enter the ticket creation view, when I want to send the ticket I get the message: "I want to send the ticket":

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE. 

I would like to know how to solve this because from what I had understood the resources routes gave you the possibility to use all the functions within them .

In my form method I have put post because it is supposed to be safer than get.

View:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Dashboard</div>

                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif

                   <form method="post">
                       @csrf
                       <label>Titulo:</label><br>
                       <input type="text" name="name"><br>
                       <label>description:</label><br>
                       <textarea name="description">Enter text here...</textarea><br>
                       <button type="submit">Enviar</button><br>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Ticket;

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

         $tickets = Ticket::all();
        return view('tickets',compact('tickets'));
    }

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

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

        $ticket = new Ticket();

        $ticket->name = request('name');
         $ticket->description = request('descripcion');

        $ticket->save();

        return redirect('/tickets');
    }

    /**
     * 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)
    {
        //
    }
}

Thank you very much

0 votes

Please add the view code

0 votes

@Apprentice ready there I edited!

0voto

Aprendiz Points 1310

When you make use of a controller of type restful creates you a path for each verb; i.e:

  • GET
  • POST
  • PUT/PATCH
  • DELETE

This is how it will be:

GET         /tickets                   index         tickets.index
GET         /tickets/create            create        tickets.create
POST        /tickets                   store         tickets.store
GET         /tickets/{tickets}         show          tickets.show
GET         /tickets/{tickets}/edit    edit          tickets.edit
PUT/PATCH   /tickets/{tickets}         update        tickets.update
DELETE      /tickets/{tickets}         destroy       tickets.destroy

In your case, since you are doing an update, the required verb is POST therefore the opening label of your form should look like this:

<form action="{{ tu ruta }}" method="POST">
    @csrf
  • So the browser receives a request POST

  • At your controller level you should have in the method store() the creation of the new registry.

  • Adds the directive @csrf to validate your request and to proceed

  • the action of your form should be aimed at the name that receives your URL that will use the store that is to say:

Label form

<form action="{{ route('tickets.store') }}" method="POST">

It is not that GET is less secure than POST is that the first one helps me to make a request and get the data and the second one helps me to make a record, modify it, etc.

Here you can read more about it

0 votes

Thank you very much but what I am doing is not updating but creating a new ticket so I would have to use the post method.

0 votes

Please show then in your question the code of the method in your controller where you try to do the registration @TomásBlanco, because at least in your code I don't see that with the form you point somewhere.

0 votes

here I added the controller to the original message

0voto

sntlln93 Points 26

You are missing the action attribute in the form tag, you should have something like this:

<form action="/tickets" method="POST">
@csrf
...
</form>

Apart from this detail you have another problem in the controller and that is that you are missing the "$" in front of request. You should also validate your request to avoid integrity conflicts later on. Here is an example of how you should do it:

public function store(){
        $request->validate([
           'name' => 'required|alpha|min:4|max:20',
           'description' => 'required|alpha|min:4|max:20',
        ]);
        $ticket = new Ticket();

        $ticket->name = request('name');
         $ticket->description = request('descripcion');

        $ticket->save();

     return redirect('tickets');
}

This is just an example, you should modify the validation rules according to your needs. However, check the documentation for more details: https://laravel.com/docs/6.x/validation

0 votes

You mean the Request class? If so, I warn you that there is no weight sign there.

0 votes

Oh, my bad. I thought there was a request in the function argument and that one was used.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X