Luiz Santa
Luiz Santa

Reputation: 11

When I use the return back(), I get an error

I have a modal form in a page pointing to a route that includes an message to a client, and it does all right, but when it is going back to the page I got the message of wrong method :

The GET method is not supported for this route. Supported methods: POST

The form is :

<form action="{{route('incluir_mensagem')}}" method="post">
                
                @csrf
               
                <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                  <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                      <div class="modal-header cab-msg">
                        <h5 class="modal-title" id="staticBackdropLabel">Nova mensagem para : {{$necps->nome_part}}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                      </div>
                      <div class="modal-body">
                          <div class="texto-oferta">Oferta : {{$ofps->desc_of}}</div>
                          <div class="texto-necessidade">Necessidade : {{$necps->desc_nec}}</div>
                          <br>
                          <textarea name ="mensagem" class="form-control" id="mensagem" rows="3"></textarea>

                          <input value="{{$ofps->id_part}}" name="id_part_t" id="id_part_t" type="hidden">
                          <input value="{{$ofps->id_of_part}}" name="id_of_part_t" id="id_of_part_t" type="hidden">
                          <input value="{{$necps->id_nec_part}}" name="id_nec_part_t" id="id_nec_part_t" type="hidden">
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Sair</button>
                        <button type="submit" class="btn btn-primary">Enviar</button>
                      </div>
                    </div>
                  </div>
                </div>

              </form>

The route is :

Route::post('incluir_mens_trans',  [TransacoesController::class,'incluir_mens_trans'])->name('incluir_mensagem');

and the controller is :

public function incluir_mens_trans(Request $request){

$confere = DB::table('transacoes')->where('id_nec_part', request('id_nec_part_t'))
                                                ->where('id_of_part', request('id_of_part_t'))

                                                ->first();

            if(!$confere) {                                                   

                  $trans = DB::table('transacoes')->insertGetId([
                        'id_nec_part' => request('id_nec_part_t'),
                        'id_of_part' => request('id_of_part_t'),

                        /*Em andamento */
                        'id_st_trans' => 2, 

                        'data_inic' => date('Y-m-d H:i:s')
                  ]);

                  $id_trans = $trans;

            }else{
                  $id_trans = $confere->id;
            }

            $msgs_i = DB::table('mensagens_trans')->insert([
                  'id_trans' => $id_trans,
                  'mensagem' => request('mensagem'),
                  'data' => date('Y-m-d H:i:s')
                  
            ]);

            $msgs = DB::table('mensagens_trans')->where('id_trans', $id_trans)
                                                ->select('mensagens_trans.*')
                                                ->orderBy('data','desc')
                                                ->paginate(5);

            return back();

Some tip about what is going on? Thanks!

Upvotes: 1

Views: 95

Answers (5)

Luiz Santa
Luiz Santa

Reputation: 11

I solved the problem using GET in the form that sent me to the page that is giving the issue. Now I can use the modal that is in this page that point to a function in a controller that uses "return back();" now with no error showing. But I got a new one. In this page I have a table showing the records there are the result of the modal inclusion in this table, and there is a paginator that is not working.

<div class="pagination">
        {{$msgs->links('layouts.paginationlinks')}}
</div>

"$msgs" came from the function that is in the controller like showed above in this Question. I dont need to resend the $msgs variable because it is already in the page.

The page has the following paramesters that came from the previus GET form :

http://localhost:8000/mens_trans_ofertas_part?_token=myYfwIbbHIZhPMiCK7j3EJpej1jHF1tgDcrN48pQ&id_part_t=26&id_nec_part_t=4&id_of_part_t=14

But when I use the pagination link, it cames with :

http://localhost:8000/mens_trans_ofertas_part?page=2

So there is no more parameters neither the token. All desapears!

I used this pagination routines in other pages with no problems. Can't understand why doenst work now. :-(

Upvotes: 0

Harshana
Harshana

Reputation: 5476

It seems like Laravel is trying to navigate back to the Form POST route. You can try redirecting with the route name or route URL:

return redirect()->route('yourRouteName');

If you want to send a message back to the client, you can use a syntax like below:

return redirect()->route('yourRouteName')->with('status', 'Profile updated!');

Reference: HTTP Redirects - Laravel

Upvotes: 0

Dyos
Dyos

Reputation: 381

try to clear all your config with php artisan optimize:clear I cannot reproduce this bug. Try to tell us the Laravel version if it persists.

Upvotes: 1

Atika
Atika

Reputation: 1067

Try to change :

return back();

By :

return redirect()->back();

Upvotes: 0

Meysam Zandy
Meysam Zandy

Reputation: 296

you should just write the method variable as uppercase:

method="POST" replace this:

<form action="{{route('incluir_mensagem')}}" method="post">

with this

<form action="{{route('incluir_mensagem')}}" method="POST">

Upvotes: 0

Related Questions