【问题标题】:How to redirect back to previous page after post request in laravel?在laravel中发布请求后如何重定向回上一页?
【发布时间】:2021-09-03 08:42:30
【问题描述】:

我有一个系统,用户在该系统中输入从和到的日期范围,并获取该日期范围之间的所有必需数据。从数据库中检索的数据显示在每行包含复选框的表中。当用户选中复选框并将其更新的数据提交到数据库时,但我必须在更新数据库中的数据后重定向到同一个表。它说这条路线不支持 GET 方法。支持的方法:POST。需要帮助。

这是我的日期范围填充视图。

@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

这是我的搜索表视图

@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

这是我的控制器

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());
  }
}

还有我的路线

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

【问题讨论】:

  • 你想重定向回这条路线Route::post('po',[PaymentController::class, 'po']);?如果是这样,那么你不能
  • 应该是简单的return back()-&gt;with(...);您没有指定要重定向到的路线,因此您没有任何理由收到该错误。你从哪条路线发帖?典型的流程是 GET 请求查看表单 -> POST 请求 -> REDIRECT -> GET 到您发布的表单。
  • @Espresso 那么我怎样才能让用户重定向到表格页面...
  • return redirect()-&gt;route('route_to_table_page'),但目前没有理由让您收到该错误。
  • @TimLewis 一个问题......页面如何知道我第一次插入的相同日期范围以在刷新后到达表格。

标签: laravel laravel-8 laravel-routing


【解决方案1】:

您的代码需要进行一些格式化并制作更易于理解的变量,但无论如何。您需要在 foreach 之前检查验证是否失败。 return redirect()-&gt;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();
}

【讨论】:

  • 这看起来很棒,不过有 1 个注释,return back()return redirect()-&gt;back() 的快捷方式;使代码更短/更容易阅读?
  • 我说“此路由不支持 GET 方法。支持的方法:POST。”
  • @TimLewis 确实如此,刚刚尝试过。我想我没有赶上最新消息:D
  • 那么现在该怎么办...有什么解决办法吗?
  • 根据您的代码判断我不确定这一点,但我相信这是因为您的 foreach 中的 $data->save()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 2016-02-10
  • 2017-04-18
  • 2019-06-08
  • 2018-06-11
  • 2016-06-18
相关资源
最近更新 更多