[英]Laravel get value form search box and query
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')); }
I think best way for newbies to do stuff like this is to use Laravel Collective Forms & HTML package (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');
将路线更改为
Route::get('/search/{id}', 'IndexController@search');
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 :) 我知道这个问题很老,但希望它能对像我这样的人有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.