如何修复 MethodNotAllowedHttpException 错误分页 laravel



我创建了一个 searsh 视图,我可以转到另一个页面中的值并且它工作正常,我可以看到我的值,但是当我添加分页时它再次工作,但是当我按下痛苦的按钮 2 时,它不起作用,它给了我一个错误: RouteCollection 中的 MethodNotAllowedHttpException .php第 251 行 at 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 中发送 searsh 请求的人

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

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

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

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

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

编辑:要进行搜索:

将您的路线更改为 GET。

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

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

<form id="verification-form-{{ $exam->id }}" action="{{ route('display-num-search', $exam ) }}" method="GET">

最新更新