我正在创建一个搜索功能,我正在尝试使用搜索表单使id出现在url上。 当我手动输入search?id=1时,它得到了正确的项目,但当使用我的表单时,它似乎没有得到id,也没有将其添加到url的末尾,不知道我做错了什么,但将感谢您的帮助。
路线
Route::get('/search', 'HomeController@search')->name('search');
Route::resource('home', 'HomeController');
控制器
public function search(Request $request){
$post = Post::where('id', $request->id)->first();
$posts = Post::all();
if (Post::where('id', '=', Input::get('id'))->exists()) {
return view('Frontend::pages.press.show', compact('post', 'posts'));
}else if (Post::where('id', '=', 0)) {
return view('Frontend::pages.press.press', compact('post', 'posts'));
}
}
查看
<form method="get" action="{{ route('search') ?? ''}}">
<div class="md-form">
<label style="font-weight: 600; display: contents;" for="form2" class="active white-text">Vehicles in stock</label>
<select style="margin-top: 0.5rem;" class="form-control search-form-control white-text">
<option id="0" value="volvo">Any vehicle...</option>
@foreach($posts as $post)
<option id="{{$post->id}}" value="volvo">{{$post->vehicle}} {{$post->h1}}</option>
@endforeach
</select>
</div>
<div class="text-center mt-4">
<button style="background-color: #2d3e50!important;" class="btn btn-indigo">Search</button>
<hr class="mb-3 mt-4 hr-light">
<div class="inline-ul text-center">
<a class="p-2 m-2 li-ic">
<i class="fab fa-linkedin-in white-text"> </i>
</a>
<a class="p-2 m-2 ins-ic">
<i class="fab fa-facebook-f white-text"> </i>
</a>
</div>
</div>
</form>
您的元素需要一个
name
属性。 另外,请确保您的具有不同的值。 将您的
更改为:
<select name="id" style="margin-top: 0.5rem;" class="form-control search-form-control white-text">
<option value="0">Any vehicle...</option>
@foreach($posts as $post)
<option value="{{$post->id}}">{{$post->vehicle}} {{$post->h1}}</option>
@endforeach
</select>
(添加了name
属性,并将{{$post->id}}
用作<选项>
的值)