Reputation: 129
I want to be able to search in laravel and display result on the same table i have displayed with all the results.
My controller is;
/*
* View all clients in descending order
*/
public function getViewClient() {
$clients = Clients::orderBy('created_at', 'desc')->paginate(15);
$clients->count();
return view('billing/clients/view', [
'clients' => $clients
]);
}
and my displayed result is
<table class="table table-bordered">
<tr>
<th>Client Number</th>
<th>Client</th>
<th>Contact</th>
<th>Country</th>
<th>Phone Number</th>
<th>Mobile Number</th>
<th>Email</th>
</tr>
@forelse($clients as $client)
<tbody>
<tr data-clientid="{{ $client->id }}">
<td>{{ $client->client_number }}</td>
<td>{{ $client->client_name }}</td>
<td>{{ $client->contact_name }}</td>
<td>{{ $client->country }}</td>
<td>{{ $client->phone_number }}</td>
<td>{{ $client->mobile_number }}</td>
<td><a href="mailto:{{ $client->email }}">{{ $client->email }}</a></td>
So now i want to implement a search bar which will search into the database and return the result in the same table above with the url bar showing the search result as well.
Please help or advice anyone.
Thanks and much appreciated
Upvotes: 0
Views: 136
Reputation: 357
1) Add Search Bar in View
<form method="GET" action="" role="search">
<div id="custom-search-input">
<div class="input-group col-md-12">
<input type="text" name="q" class="form-control" placeholder="Search Member" value="{{Request::get('q')}}">
<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>
2) Write in your Controller
$clients = Client::getClients();
$clients->count();
return view('billing/clients/view', [
'clients' => $clients
]);
3) Write in Client Model
public static function getClients()
{
$q = Request::get('q');
$clients = DB::table('clients')
->where(function ($query) use ($q) {
$query->orWhere('client_name', 'like', '%'.$q)
->orWhere('contact_name', 'like', '%'.$q)
->orWhere('phone_number', 'like', '%'.$q);
})
->orderBy('created_at', 'desc')->paginate(15);
return $clients;
}
Upvotes: 1