melx's avatar
Level 4

laravel redirect with post method

i have the route which fetch the data when i send the request,

with that data in the blade view i have the button to select the ID and update

it update well but am get this error

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

In my web

       //fetch data 
   Route::post('sales_reports_by_team','ReportController@reportbyteam')->name('reports.team');

  //select bill_number and update the status=1
 Route::post('/audit_bill/{bill_number}','ReportController@auditbillnumber')->name('audit.bill');

My controller

             public function auditbillnumber(Request $request,$bill_number)
           {
 

            DB::table('sales')->where('sales.bill_number', $bill_number)->update(['audit_status'=>1]);
           return redirect()->back()->with('alert','Bill has been updated Successfully');

            }
0 likes
7 replies
tykus's avatar

Show how you have implemented the button in the view; it appears you are sending a GET request because either (i) you did not add the POST method attribute on a form, or (ii) you are using an a element to a POST route.

melx's avatar
Level 4

the button

                         <td> <button class="btn  btn-succes audit" data-id="{{$dt->bill_number}}">AUDIT BILL</button></td>



          $(document).ready(function(){
    $(".audit").click(function (e) {
      var bill_number = $(this).data("id");

       if(!confirm("Do you really want to Audit this?")) {
                   return false;

                 }

        e.preventDefault();
  var token = $("meta[name='csrf-token']").attr("content");

        $.ajax({
            url: "/audit_bill/"+bill_number,
            type: "POST",

             data:{bill_number:bill_number,
               token:token,
                },
            dataType: "json",
            success: function (data) {
                console.log('SUCCESS: ', data);
                // location.reload();
                return false;
            },
            error: function (data) {
                console.log("ERROR: ", data);
                // location.reload();
                return false;
            },
        });
    });
     });
automica's avatar

@emfinanga your button is firing a POST request via ajax.

API endpoints shouldnt redirect you anywhere. You should just return a 200 success response from your auditbillnumber method.

melx's avatar
Level 4

@automica , i need to get the alert on the same route to show successfully

automica's avatar

correct. send a success/ fail response back from your post method, and your js can decide what message to display.

Snapey's avatar

You are doing an ajax call. You should not be redirecting back at the end of the function. Return a response appropriate for your javascript to consume, eg a success or failure state

You can then show a message from your javascript.

If this is too much for you, dont use ajax, just do a regular form submission

Snapey's avatar

And put in some security. At present its possible for anyone to change the status of any bill

Please or to participate in this conversation.