在laravel的show方法中,我有一个表单要提交并在同一页面上显示结果,所以这里首先是我的show方法:
public function show(Property $property)
{
$property = Property::with('propertycalendars')->where('id', $property->id)->first();
foreach ($property->propertycalendars as $prop) {
$end_reserve = $prop->reserve_end;
}
// HERE NEW RELATION
$pdate = Property::with('dates')->get();
return view('users.properties.show', compact('property','pdate','end_reserve'));
}
在我的节目视图中,例如,这是uniq属性的网址,如下所示:
http://localhost:8000/properties/1
现在我有一个表单可以搜索日期表并将日期带给我,所以这就是我为搜索功能编写的内容:
public function search (Request $request,$property_id){
//Send an empty variable to the view, unless the if logic below changes, then it'll send a proper variable to the view.
$results = null;
//Runs only if the search has something in it.
if (!empty($request->property_id)) {
$start_date = $request->start_date;
$search_date = Date::all()->where('date',$start_date);
}
return view('admin.properties.show')->with('search_date', $search_date);
}
和 多数民众赞成在我的路线:
Route::get('/properties/{{property_id}}','PropertyController@search');
最后是我的表单以提交搜索:
<form action="/properties/search" method="get">
{{csrf_field()}}
<div class="row">
<div class="col-lg-5">
<input type="hidden" value="{{$property->id}}" name="property_id">
<input name="start_date" class="form-control m-input start_date" autocomplete="off"> </div>
<div class="col-lg-5">
<input name="finish_date" class="form-control m-input start_date" autocomplete="off"> </div>
<div class="col-lg-2">
<input type="submit" value="seach" class="btn btn-primary btn-block" autocomplete="off">
</div>
</div>
</form>
但是现在当我提交表单时,它会返回一个未找到的404,如下所示:
http://localhost:8000/properties/search?_token=R8ncSBjeZANMHlWMcbC6o5mYJZfwWgdfTwuviFo1&property_id=1&start_date=1398%2F1%2F12&title=
答案 0 :(得分:3)
在您的控制器中,更改为以下内容:
public function search (Request $request){
//Send an empty variable to the view, unless the if logic below changes, then it'll send a proper variable to the view.
$results = null;
//Runs only if the search has something in it.
if (!empty($request->title)) {
$results = Property::all()->where('some search here')->get();
}
return view('admin.article.index')->with('results', $results);
}
这会将查询找到的所有(和所有)结果发送到视图。现在,在您的视图中,您需要确保有实际结果,否则会得到错误,例如:
@if ($results)
//There are results, loop through them
@foeach($results as $item)
{{$item->title}}
@endforeach
@else
//There are no results, show the form maybe?
@endif
在不知道您的表结构的情况下,我无法给出确切的方法来遍历您的结果,但这应该可以帮助您入门。
编辑:由于OP的问题性质与原始问题有所不同:
为了实现新流程,您将需要在路由中传递一个URL参数,并将其更改为get,因为您不再从表单中发布它:
Route::get('/properties/{search}','PropertyController@search');
这告诉Laravel您从website.com/properties/xxxxx
请求中得到了一些东西-xxxxx
将包含您要传递给控制器以进行查找的搜索关键字。路由中的{search}
部分可以是您想要的任何名称,只需确保控制器的第二个参数与之匹配即可。
如果您希望允许从搜索表单中发布信息,则可以(另外)将以下内容添加到您的路线中:
Route::post('/properties','PropertyController@search');
然后在您的控制器中,通过Request门面获取来自表单的所有内容。
然后在您的控制器中,检查这是否有效:
public function search (Request $request, $search){
//Send an empty variable to the view, unless the if logic below changes, then it'll send a proper variable to the view.
$results = null;
//Runs only if the second URL param has a value
if (!empty($search)) {
$results = Property::all()->where('some search here')->get();
}
return view('admin.article.index')->with('results', $results);
}