【问题标题】:How to fix MethodNotAllowedHttpException error pagination laravel如何修复 MethodNotAllowedHttpException 错误分页 laravel
【发布时间】:2018-05-21 13:23:55
【问题描述】:

我已经创建了一个搜索视图,我可以转到另一个页面中的值并且它工作正常,我可以看到我的值但是当我添加分页时它再次工作但是当我按下疼痛的 2 号按钮时它不工作它给了我一个错误: RouteCollection.php 第 251 行中的 MethodNotAllowedHttpException 在 RouteCollection->methodNotAllowed(array('POST')) ...

我的功能:

public function searsh()
  {
    return view('resultats.recherche');
  }


 public function show(Request $request)
  {

    $this->validate($request, [
      'cne' => 'required|integer',
    ]);

    $exams = Exam::where('cne', $request->cne)->paginate(4);
    return view('resultats.index', compact('exams'));
  }

这是我的路线: 第一个让我在 db 中发送搜索请求的请求

  Route::get('/chercher', 'ExamsController@searsh')->name('display-cne-search');
Route::post('/resultat', 'ExamsController@show')->name('show-exams-cne-results');

这是我的观点

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">

<style>

body{
  background-repeat:no-repeat;
  -webkit-background-size:cover;
  -moz-background-size:cover;
  -o-background-size:cover;
  background-size:cover;
  background-position:center;
background: url('image/t10.jpg');
}
.bling{
  background:url('image/background.jpg');
}
</style>
@extends('layouts.master')
@section('content')

@if($exams->isEmpty())
<div style="width:100%;height:100px;background-color:white">
<center>
  <p style="margin-top:50px;">Le cne que vous avez choisi est incorrecte entrer un autre cne svp <a href="/chercher">
    <br>
  <button  style="margin-top:5px"class="btn btn-primary">Par ici</button></a>
  </p>
</center>
</div>
@else
<div class="container">
  <div  class="row">
    <center><div class="col-md-12">
      <div style="border-radius:5px;background:url('image/background.jpg')">
      <center>
        <h2 style="color:white;font-weight: bold;padding-top:5px;padding-bottom:5px;" > Historique des résultats de l'étudiant(e) {{$prenom}} {{$nom}} <br>
           CNE {{$cne}}</h2>


      </center>
      </div>
      <center><table style="background-color:white;border-radius:5px;"  class="table">
        <head>
        <tr>
          <th>Matiére</th>
          <th>Note</th>
          <th>Semestre</th>
          <th>Session</th>
          <th>Année</th>
          <th>Le prof vous a peut-être laissé une remarque</th>
          <th>Demande Vérification</th>
        </tr>
      </head>
      <body>
        @foreach($exams as $exam)
        <tr  class="{{$loop->index % 2==0 ? 'bling' : ''}}">
          <td>{{ $exam->matiere}}</td>
          <td>{{ $exam->note}}</td>
          <td>{{ $exam->sem}}</td>
          <td>{{ $exam->ses}}</td>
          <td>{{ $exam->an}}</td>
          <td>
          <center>
          <button type="button" value="{{$exam->remarque}}" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter" onClick="document.getElementById('remarque').innerHTML=this.value">
            Remarque
          </button>
          </center>
          </td>
         <td>
          @if(!$exam->verification)
        <button  class="btn btn-succees" onclick="document.getElementById('verification-form-{{$exam->id}}').submit();" >
            Vérifier la note
          </button>
        <form id="verification-form-{{ $exam->id }}" action="{{ route('display-num-search', $exam ) }}" method="post">
          {{ csrf_field() }}
        </form>
          @elseif($exam->verification->etat=="En attente")
            <center> <a title="{{$exam->verification->etat}}" style="background-color:orange" class="btn-floating btn pulse"></a></center></p>
          @else
            <center><p> Verifié <i title="Note verifié par le professeur" class="fas fa-user-check"></i></p></center>
          @endif
        </td>
        </tr>
        @endforeach
        <center>{{ $exams->links()}}</center>
      </body>
    </table></center>
<!-- modal remarque -->
      <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="exampleModalLongTitle">Remarque du prof</h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <div style="font-size:15px;font-weight: bold;" id="remarque"></div>
            </div>

          </div>
        </div>
      </div>
    </div></center>
  </div>
</div>

<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous">

  document.addEventListener('DOMContentLoaded', function() {
    var elems = document.querySelectorAll('.dropdown-trigger');
    var instances = M.Dropdown.init(elems, options);
  });

  // Or with jQuery

  $('.dropdown-trigger').dropdown();

</script>

@endif
@endsection

【问题讨论】:

  • {{$exams-&amp;gt;render()}} 怎么样?更新:把你的链接放在表格代码之外。
  • 我已将其从表格中取出,但仍无法使用渲染或链接
  • 搜索完成后能否粘贴页面的url
  • 如果表关闭后放置
  • 从第一个视图搜索后链接 fsjes2.oo/resultat 在我点击分页中的数字 2 后,我得到带有错误页面的 fsjes2.oo/resultat?page=2

标签: php laravel pagination


【解决方案1】:

POST 每次都需要一个 CSRF 令牌来处理请求。

因此,为了执行搜索并获取分页,您可以将路线设置为:

Route::any('/resultat', 'ExamsController@show')-&gt;name('show-exams-cne-results');

通过这种方法,执行的第一次搜索使用带有 CSRF 令牌的 POST 方法处理,然后对于分页,使用 GET 方法。

编辑:进行搜索:

将您的路线更改为 GET。

Route::get('/resultat', 'ExamsController@show')-&gt;name('show-exams-cne-results');

另外,将表单方法更改为 GET

&lt;form id="verification-form-{{ $exam-&gt;id }}" action="{{ route('display-num-search', $exam ) }}" method="GET"&gt;

【讨论】:

  • 当我设置它而不是发布页面时,任何都不起作用当我点击分页中的数字 2 时页面不再加载
  • 好的,我认为您应该使用 GET 进行搜索。
  • 我应该换个帖子来获取吗?
  • 是的,更改帖子以进入您的路线。我已经更新了我的答案,请重新加载。
  • 我厌倦了它,当我想访问我在第一个视图中单击搜索后得到的视图(1/1)MethodNotAllowedHttpException 时它也不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-07
  • 2017-09-23
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
相关资源
最近更新 更多