如何将文本框值作为搜索项发送到laravel中的控制器



我有一个单词表,我想在索引视图中保留一个搜索表单来搜索想要的单词。

这是我为在控制器中搜索而写的函数。

public function search(string $id){                
if($id!="")
{
$terms = Terms::query()
->where('term', $id)           
->get();
if(count($terms)==0)
{
$terms = Terms::query()
->where('term', 'LIKE', "{$id}%")
->orWhere('term', 'LIKE', "%{$id}%")
->get();
}
if(count($terms)==0)
{
$terms = Terms::query()           
->orWhere('term', 'LIKE', "%{$id}%")
->get();
}
return view('admin/terms/search')->with('terms', $terms);
}       
}

这是中我的表单代码

{!! Form::open(['action' => 'AppHttpControllersTermsController@search', 'method' => 'GET']) !!}
<form class="bd-forms">

<div class="form-group">
{{ Form::label('word', 'Search word') }}
{{ Form::text('word', '', ['class' => 'form-control', 'placeholder' => 'type word here']) }}
@error('word')
<div class=""
style="padding-right:5px;margin-top:5px;margin-bottom:10px; color:red;font-size:12px;">

the field is empty.
</div>
@enderror
</div>          
<div>
{{ Form::submit('Search', ['class' => 'btn btn-primary']) }}
</div>
</form>
{!! Form::close() !!}

现在我想知道如何将文本值作为$id参数发送到搜索函数,方法应该是"GET"还是"POST"?

您可以使用Get方法进行类似过滤器的操作,只需发送请求

public function search(Request $r){ 
$filter=$r->query('id');               
if(!empty($filter))
{
$terms = Terms::query()
->where('term', 'like', '%'.$filter.'%')           
->get();
$terms->appends($request->all());
}
else
{
$terms = Terms::get();
}
return view('admin/terms/search')->with('terms', $terms);

}

在刀片文件中,为了过滤,你必须这样做

<form class="form-inline justify-content-center" method="GET">
<input type="text" class="form-control mr-2 text-center"  name="id" placeholder="term" value="{{$id}}">
<button type="submit" >Search</button>
</form>