Laravel 获取值表单搜索框和查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36842671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel get value form search box and query
提问by Slatan-ko
am try to get value form search box and send to controller method to show object
我尝试从搜索框获取值并发送到控制器方法以显示对象
i try to use get method form but it not work.
我尝试使用 get 方法形式,但它不起作用。
here is my view
这是我的观点
<form method="GET" action="search" accept-charset="UTF-8">
<div id="custom-search-input">
<div class="input-group col-md-12">
<input type="text" class="form-control input-lg" placeholder="enter word" />
<span class="input-group-btn">
<button class="btn btn-info btn-lg" type="submit">
<i class="glyphicon glyphicon-search"></i>
</button>
</span>
</div>
</div>
</form>
and this is my route
这是我的路线
Route::get('/search', 'IndexController@search');
Route::controller('index', 'IndexController');
this is my controller
这是我的控制器
public function getIndex() {
$words = Word::all();
return view('dict.index', compact('words'));
}
public function search($id) {
$words = Word::find($id);
if (empty($article)) {
abort(404);
}
return view('dict.index', compact('words'));
}
回答by Alexey Mezenin
I think best way for newbies to do stuff like this is to use Laravel Collective Forms & HTMLpackage (which was a part of Laravel 4).
我认为新手做这样的事情的最好方法是使用Laravel Collective Forms & HTML包(这是 Laravel 4 的一部分)。
{!! Form::open(array('method' => 'Get', 'route' => array('route.name', $variable))) !!}
{!! Form::text('search') !!}
<button>Search</button>
{!! Form::close() !!}
And in controller, you can access variable with:
在控制器中,您可以通过以下方式访问变量:
public function search(Request $request)
{
$request->get('search');
回答by WhSol
Change route to
将路线更改为
Route::get('/search/{id}', 'IndexController@search');
回答by Yousef Altaf
lets start with the Routes
让我们从 Routes
Route::get( '/search', 'IndexController@search' )->name('search');
Now see the controller
现在看到控制器
$words = ( new Words )->whereHas( 'translations', function ( $q ) use ( $keywords ) {
$q->where( 'name', 'like', '%' . $keywords . '%' )
->orWhere( 'details', 'like', '%' . $keywords . '%' );
} )->get();
and here as @AlexeyMezenin suggested
正如@AlexeyMezenin 建议的那样
{!! Form::open([
'route'=>['search'],
'method'=>'get']) !!}
{!! Form::text('keywords', null, ['placeholder'=>'What are you looking?', 'class'=>'form-control' ]) !!}
<button><i class="fa fa-search"></i></button>
{!! Form::close() !!}
I know its very old question but hope it helps someone like me :)
我知道这是一个非常古老的问题,但希望它可以帮助像我这样的人:)