sudeep Acharya
sudeep Acharya

Reputation: 55

How to redirect back to previous page after post request in laravel?

I have a system where user enters the date range from and to and gets all required data between that date range. Data retrieved from the database shows in the table containing checkbox to each row. When user Check the check box and submit the data it updates to the database, but I'm have to redirect to the same table after updating the data in database. It says the GET method is not supported for this route. Supported methods: POST. Need Help.

Here is my Date range fill view.

@extends('master')
@section('content')
<div class="input-group" style="margin:50px">
  <table>
    <form action="/payment_list_table" method="POST">
      @csrf
      <div class="form-outline">
        <tr>
          <th>
            <label>From</label>
          </th>
          <th>
            <input type="date"  name= 'from' class="form-control" placeholder="Doc Received Date" required="" />
          </th>
          <th>
            <label>To</label>
          </th>
          <th>
            <input type="date"  name= 'to' class="form-control" placeholder="Doc Received Date" required="" />
          </th>
        </tr>                        
        <th>
          <button type="submit" class="btn btn-success">
            <i class="fas fa-search"></i>
          </button>
        </th>
      </div>
    </form>
  </table>
</div>
@endsection

Here is my Search Table View

@extends('master')
@section('content')
<div class='container' style="margin-top:50px">
  <div class="row">
    <div class="input-group" style="margin:20px">
      <form>
        <table style="float:right">
          <th>
            <div class="form-outline">
              <input type="search" id="form1" class="form-control" placeholder="Search"/>
          </th>                        
      </form>
    </div>
    <form method="POST">
      @csrf
      <button type="submit" formaction="#" name="submit" class="checklistpo">PO</button>
      <button type="submit" formaction="#" name="submit" class="ajax.po">AO</button>
        <div class="table-responsive">
          <table class="table custom-table">
            <thead>
              <tr>
                <th scope="col">
                  <label class="control control--checkbox">
                    <input type="checkbox" class="js-check-all"/>
                    <div class="control__indicator"></div>
                  </label>
                </th>
                <th scope="col" >S.N</th>
                <th scope="col">LC NO</th>
                <th scope="col">Applicant</th>
                <th scope="col">Doc Value</th>
                <th scope="col">Doc Received date</th>
                <th scope="col">LC Type</th>
                <th scope="col">Action</th>
              </tr>
            </thead>
            <tbody>
              <?php $number = 1;?>
              @foreach($datas as $items)
                <tr>
                  <td><input type="checkbox" name="checkboxlist[]" value="{{$items->id}}" /></td>
                  <td>{{$number}}</td>
                  <td>{{$items->lc_no}}</td>
                  <td>{{$items->applicant}}</td>
                  <td>{{$items->doc_value}}</td>
                  <td>{{$items->rec_date}}</td>
                  <td>{{$items->sight_usance}}</td>                 
                </tr>
                <?php $number++; ?>
              @endforeach
            </tbody>
          </table>
        </div>
      </form>
    </div>
  </div>
  @endsection

Here is my Controller

function po(Request $req){
  $validate=Validator::make($req->all(), [
    "checkboxlist" => "required|array"
  ]);

  foreach($req->checkboxlist as $list){
    $data = Doc::find($list);
    $data->payment_comment = "PO";
    $data->Payment_status = "Prepared";
    $data->save();
  };

  return Redirect::back();
    
  if($validate->fails()){
    return redirect()->back()->with("errors",$validate->errors());
  }
}

And My Route

Route::post('po', [PaymentController::class, 'po']);

Upvotes: 0

Views: 2133

Answers (1)

Kevin
Kevin

Reputation: 1185

Your code needs a bit of formatting & make more understandable variables but anyways. You need to check if validation fails before foreach. return redirect()->back(); is the right way to go back.

function po(Request $req)
{
    $validate = Validator::make($req->all(), [
        "checkboxlist" => "required|array"
    ]);

    if ($validate->fails()) {
        return redirect()->back()->with("errors", $validate->errors());
    }
    
    foreach ($req->checkboxlist as $list) {
        $data = Doc::find($list);
        $data->payment_comment = "PO";
        $data->Payment_status = "Prepared";
        $data->save();
    }

    return redirect()->back();
}

Upvotes: 2

Related Questions