我想创建功能性搜索框来查询某个表中的数据。
数据库详细信息:
搜索框详细信息:
叶片/ HTML
{{ Form::open(array('action' => 'UrlController@query', 'method' => 'get')) }}
<input autofocus name="search" id="search" placeholder="Enter Catalog # " type="text" />
<span class="pull-right">
{{ Form::submit('Search', array('class' => 'btn btn-success btn-sm')) }}
</span>
{{ Form::close() }}
如您所见,我的搜索框名称为search
控制器功能:
public function query() {
// grab GET parameters
$search = Input::get('search');
// apply search filter
if ($search){
//$inventories = Inventory::all(); # It's works if I do this
$inventories = Inventory::where(function($inventories) use ($search){
$inventories->where('search', 'like', '%'.$search.'%');
});
return View::make('inventories.index')
->with('inventories', $inventories);
}else
return View::make('inventories.index');
}
最后:
我无法显示任何结果。
我做错了什么吗?在这里:
$inventories = Inventory::where(function($inventories) use ($search){
$inventories->where('search', 'like', '%'.$search.'%');
});
在Laravel中完成此类任务的最有效方法是什么?
答案 0 :(得分:1)
您没有使用 get()方法
$inventories = Inventory::where(function($inventories) use ($search){
$inventories->where('search', 'like', '%'.$search.'%');
})->get();
并使用 paginate()方法
$inventories = Inventory::where(function($inventories) use ($search){
$inventories->where('search', 'like', '%'.$search.'%');
})->paginate(15);
希望它会有所帮助
答案 1 :(得分:0)
尝试这个,它适用于我&gt;&gt;
$ columns = Schema :: getColumnListing(&#39; inventoryories&#39;);
$query = User::query();
$search = Input::get('search');
foreach($columns as $column){
$query->orWhere($column, 'like', '%'.$search.'%');
}
$inventories = $query->get();
return $inventories;