将返回视图更改为返回路线的laravravel搜索功能



我试图纠正我的控制器的搜索结果函数的代码,所以我担心的是,当我分页搜索结果或进行表单验证时,我得到以下错误消息:get方法不支持此路由。支持的方法:POST。如果你能帮助我,我想把代码从返回视图改为返回路由。这是我的代码

路线:

Route::get('/post/searchpost', 'PostsController@index')->name('index');
Route::post('/post/search_result', 'PostsController@searchresult')->name('searchresult');

叶片//索引

<div class="posts">
<h1>Posts</h1>
<div class="sreach">
<form action="{{route('searchresult', app()->getLocale())}}" method="POST">
{{ csrf_field()}}
<div class="form-row">
<div class="form-group col-md-2">
<label class="lbl" for="cat_id">title</label>
<select class="form-control" id="cat_id" name="cat_id">
<option value="" selected></option>
@foreach ($categoriesas $cat)
<option value="{{$cat->id}}">{{$cat->cat_mame}}</option>
@endforeach
</select>
</div>
<div class="form-group col-md-2">
<label class="lbl" for="title">Post title</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group col-md-2">
<label class="lbl" for="content">Post content</label>
<textarea class="form-control" id="content" name="content">
</textarea >
</div>
</div>
<center>
<button type="submit">search</button>
</center>
</form>
</div>
<table id="posts">
<thead>
<tr>
<th>{{__('main.title')}}</th>
<th>{{__('main.post_category')}}</th>
<th>{{__('main.content')}}</th>
</tr>
</thead>
<tbody>
@foreach($posts as $postkey => $post)
<tr>
<td>{{$post->title}}</td>
<td>{{$post->category->cat_name}}</td>
<td>{{$post->content}}</td>
</tr>
@endforeach
</tbody>
</table>

叶片//searchresult

<div class="posts">
<h1>Posts</h1>
<div class="sreach">
//that make me search again in result page using the same form
<form action="{{route('searchresult', app()->getLocale())}}" method="POST">
{{ csrf_field()}}
<div class="form-row">
<div class="form-group col-md-2">
<label class="lbl" for="cat_id">title</label>
<select class="form-control" id="cat_id" name="cat_id">
<option value="" selected></option>
@foreach ($categoriesas $cat)
<option value="{{$cat->id}}">{{$cat->cat_mame}}</option>
@endforeach
</select>
</div>

<div class="form-group col-md-2">
<label class="lbl" for="title">Post title</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group col-md-2">
<label class="lbl" for="content">Post content</label>
<textarea class="form-control" id="content" name="content">
</textarea >
</div>
</div>
<center>
<button type="submit">search</button>
</center>
</form>
</div>

<table id="posts">
<thead>
<tr>
<th>{{__('main.title')}}</th>
<th>{{__('main.post_category')}}</th>
<th>{{__('main.content')}}</th>
</tr>
</thead>
<tbody>
@foreach($details as $detailkey => $detail)
<tr>
<td>{{$detail->title}}</td>
<td>{{$detail->category->cat_name}}</td>
<td>{{$detail->content}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>

控制器

public function index()
{
$ctegories=  Category::all();
return view('search')->with('posts', Post::all())->with('ctegories', $ctegories);
}
public function searchresult(Request $request)
{
$title= $request->title;
$content= $request->content;
$cat_id= $request->cat_id;
$posts= Post::where('title', 'like', '%' . $title. '%')
->whereHas('category', function(Builder $query) use ($sujet_id){
$query->where('id', $cat_id);
})
->orderBy('created_at','desc')
->get();
//for form dropdown categories
$categories=  Category::all();
if(count ($posts) > 0)
{
//this what i want to change from view to route with those parameters
return view('searchresult')
->withDetails($posts)->withQuery($title)
->withCategories($categories);
} else {
return redirect()->route('index')->with('success','search not found');
}
}

否则函数就能正常工作,我就能得到数据我很感激你的帮助,谢谢。

你应该使用下面的方法返回带参数的路由:

return Redirect::route('nameOfRoute', ['param1'=>'value1','param2'=>'value2',...])->with('message', 'Search found!!!');

相关内容

  • 没有找到相关文章

最新更新