我有一个表格(Laravel 5.2)进行搜索。我只有一个表,我的数据显示。如果我用lastName搜索,我希望它显示每个姓氏的人。如果我按制造商搜索,我希望它显示该制造商生产的所有PC。我基本上试图使数据库上的每一列都可搜索。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Inventory</title>
<link rel="stylesheet" href="/css/libs.css">
<link rel="stylesheet" href="/css/app.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://inventory.app:8000/computers/create" Inventory</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>
{{ Form::open(['method' => 'GET', 'url'=>'computers/search']) }}
{{ Form::input('search', 'q', null, ['placeholder' => 'Search...']) }}
{{ Form::close() }}
</li>
<li><a href="http://inventory.app:8000/computers/create">Update Inventory</a></li>
<li><a href="#">Logout</a></li>
</ul>
</div>
</div>
</nav>
我正在使用路由资源进行路由。我的搜索路线是
路由:: get(&#39;计算机/搜索&#39;,&#39; InventoriesController @ search&#39;);
我的搜索控制器如下
public function search()
{
$search = \Request::get('search'); //<-- we use global request to get the param of URI
// $search = Input::get('search');
$inventories = Inventory::where('lastName','LIKE','%'.$search.'%')
-> orwhere('firstName', 'LIKE','%'.$search.'%' )
-> orwhere('department', 'LIKE','%'.$search.'%' )
-> orwhere('building', 'LIKE','%'.$search.'%' )
-> orwhere('room', 'LIKE','%'.$search.'%' )
-> orwhere('manufacturer', 'LIKE','%'.$search.'%' )
-> orwhere('device', 'LIKE','%'.$search.'%' )
-> orwhere('model', 'LIKE','%'.$search.'%' )
-> orwhere('tag', 'LIKE','%'.$search.'%' )
-> orwhere('macAddress', 'LIKE','%'.$search.'%' )
-> orwhere('status', 'LIKE','%'.$search.'%' )
-> orwhere('comments', 'LIKE','%'.$search.'%' )
->get($search);
return view('computers.search',compact('inventories'));
}
我尝试过订单(&#39;搜索&#39;)但是没有做任何事情。我的表只是显示,但它确实重定向到搜索路由,因为URL更改 例如http://inventory.app:8000/computers/search?q=Dell
答案 0 :(得分:0)
如您的网址所示,您要查找的变量为q
,而不是search
。所以在你的请求中得到它:
$search = \Request::get('q');
由于您搜索的内容很可能不是列,因此请勿在{{1}}调用中使用它。只需获取所有列,或只是您想要的列
get()
或
->get();