I want to search keyword base on the word on table but when I search there is no output and no error display but the url display the keyword for example http://127.0.0.1:8000/users?keyword=sulaimani
Index.blade.php
<form action="{{ route('users.search') }}" method="GET">
<input type="text" name="keyword" class="form-control" placeholder="Search for...">
<button type="submit" class="btn btn-info">
<i class="fa fa-search"></i> Search
</button>
</form>
UserController.php
public function search(Request $request)
{
$keyword = $request->get('keyword');
$users = User::where('name', 'LIKE', '%' . $keyword . '%')->orwhere('email', 'LIKE', '%' . $keyword . '%')->paginate(4);
return view('users.index')->with(compact('users'));
}
web.php
Route::get('user/search', 'UserController#search')->name('users.search');
First try to find out the name or email like "sulaimani" in your users table. If they are returning what you need then come to the code.
Just dump and die the variable $keyword and see what your are getting (dd($keyword))
Or you can just change the same like:
$keyword = $request->keyword;
Now in dump and die (dd($keyword)) if you are getting your input then just remove dd and hit the following query using eloquent,
$users = User::where('name', 'LIKE', "%{$keyword}%")->OrWhere('email', 'LIKE', "%{$keyword}%")->paginate(4);
Hope this may help you.
Related
I'm trying to build search functionality where user can submit multiple keywords each one on new line in the textarea and then perform search in database table.
The problem is with my current code that I have is searching for the last keyword only.
Here is the function that I have in my controller. I'm not sure if the query should be in the foreach loop at all.
public function search(Request $request){
$search = $request->input('search');
foreach(explode("\r\n", $search) as $line) {
$result = Posts::query()
->where('title', 'LIKE', "{$line}%")
->get();
}
return view('search', compact('result'));
}
Here is the textarea on the search blade
<form role="form" id="form-buscar" action="{{ route('search') }}" method="GET">
<textarea class="form-control" type="textarea" name="search" rows="5" cols="50" placeholder="Search..." required></textarea>
<button class="btn btn-success pull-right" type="submit"><i class="glyphicon glyphicon-search" aria-hidden="true"></i> Search</button>
</form>
You need to contain like condition with foreach function instead all the query:
$search = $request->input('search');
$query = Posts::query();
foreach (explode("\r\n", $search) as $line) {
$query->where('title', 'LIKE', "{$line}%");
}
$result = $query->get();
return view('search', compact('result'));
I'm trying to make search for booking by user id but it's give me all booking for all users
I want to know how to retrive all booking by user_id from table booking,
Thanks in advance, Please Note that in offline working very well, but online give me all booking for all users.
<form action="{{url('search',Auth::user()->id)}}" method="get">
<div class="form-group">
<label for="Search">Search :</label>
<input class="form-control" placeholder="Search Here" type="text" name="search">
<button type="submit" class="btn btn-success btn-mini deleteRecord">Search</button>
</div>
</form>
</div>
Route:
Route::get('search/{id}','BookAppointController#search');
Controller:
public function search(Request $request, $id)
{
$search=$request->get('search');
$Allappoints=DB::table('bookappoitments')->where('users_id',$id)
->where('gustname','like', '%' .$search. '%')
->orwhere('gustcompany','like', '%' .$search. '%')
->orwhere('gustemail','like', '%' .$search. '%')
->join('times','bookappoitments.times_id','times.id')
->join('dates','bookappoitments.Dates_id','dates.id')
->select('bookappoitments.*','times.from_to','dates.Dates')->orderBy('times.id')
->paginate(10);
return view('admin.ManageTime.All',compact('Allappoints'));
}
Use this format, please
public function search(Request $request, $id)
{
$search=$request->get('search');
$Allappoints=DB::table('bookappoitments')->where('users_id',$id)-
>paginate(10);
return view('admin.ManageTime.All',compact('Allappoints'));
}
Tell me if any other issue.
So I have this search function for offices it can search office name but what I want is able to filter floors as well like you type in "1st Floor" it shows the offices that belong in that floor
Here's the code:
BuildingController.php
public function show($id)
{
$building = Building::find($id);
$search = \Request::get('search');
$offices = Office::where('building_id', $id)->where('name','LIKE','%'.$search.'%')->orderBy('floor')->get();
}
I tried using this kind of function // $offices = Office::where('building_id', $id)->where('name','LIKE','%'.$search.'%')->orWhere('floor','LIKE','%'.$search.'%')->orderBy('floor')->get(); to be able to search for floor as well but it screwed up my programs like it shows all the offices in every building even the one that it doesn't belong to.
building.blade.php
{!! Form::open(['method'=> 'GET','url'=>['building',$building->id],'role'=>'search']) !!}
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="search" class="form-control" placeholder="Search..." >
<span class="input-group-btn">
<button type="submit" class="btn btn-warning btn-md"><span class="glyphicon glyphicon-search"></span> Search</i>
</button>
</span>
</div>
{!! Form::close()!!}
Use the where() closure:
$offices = Office::where('building_id', $id)
->where(function($q) use($search) {
$q->where('name', 'like', '%' . $search . '%')
->orWhere('floor', 'like', '%' . $search . '%');
})
->orderBy('floor')
->get();
1) I added the search bar to the view :
{!! Form::open(['method'=>'GET','url'=>'home','class'=>'navbar-form navbar-left','role'=>'search']) !!}
<div class="input-group custom-search-form">
<input type="text" class="form-control" name="search" placeholder="Search...">
<span class="input-group-btn">
<button class="btn btn-default-sm" type="submit">
<i class="fa fa-search">i
</button>
</span>
2) In my controller I'm displaying all my users in a table and the search bar is on top of it
public function index()
{
$user = User::all();
$search = \Request::get('search'); the param of URI
$users = User::where('name','=','%'.$search.'%')
->orderBy('name')
->paginate(20);
return view('home',compact('users'))->withuser($user);
}
Here is what the table looks like
#foreach($user as $users)
<th scope="row">1</th>
<td>show</td>
<td>{{$users->name}}</td>
<td>{{$users->city}}</td>
<td>{{$users->phone}}</td>
<td>{{$users->street}}</td>
<td>{{$users->national_id}}</td>
<td>{{$users->name}}</td>
</tr>
#endforeach
What I'm trying to get is when I search in the bar I want to do a loop like this
#foreach($users as $user)
{{ $user->name }}
#endforeach
and replace the view to the searched names only.
and here is the route for the index
Route::get('/home', 'HomeController#index');
how can I achive that ? sorry for the long question in advance.
You need to use like instead of =:
$users = User::where('name', 'like', '%'.$search.'%')
->orderBy('name')
->paginate(20);
Also, you're trying to create two queries. Much better way is to create local scope:
public function scopeSearch($q)
{
return empty(request()->search) ? $q : $q->where('name', 'like', '%'.request()->search.'%');
}
And then use it in controller:
public function index()
{
$users = User::search()->orderBy('name')->paginate(20);
return view('home', compact('users'));
}
This code will paginate all users if there is no search parameter or it will filter users and paginate them.
My purpose is to get the input value to action url form.
Html:
<form method="POST" action="/search/the/{{$request->find}}">
{{csrf_field()}}
<div id="check" class="input-group margin-bottom-sm">
<input class="form-control" type="text" name="find" placeholder="Search">
<button type="submit"><div id="search" class="input-group-addon"><i class="fa fa-search"></i></div></button>
</div>
</form>
Routes:
Route::post('/search/the/{names}', 'maincontroller#search');
Route::get('/tfind', 'maincontroller#takefind');
Controller:
public function search($names, Request $request)
{
//dd($request->find);
$names = $request->input('find');
if(!empty($names)){
$find = DB::table('products')
->select('name', 'description', 'price')
->where('name', 'LIKE', '%' . $names . '%')
->orwhere('description', 'LIKE', '%' . $names . '%')
->orwhere('price', 'LIKE', '%' . $names . '%')
->get();
}
return view('layouts.search', compact('find', 'names'));
}
public function takefind(Request $request)
{
$names = $request->input('find');
return view('layouts.find', compact('names'));
}
When I put manually the wildcard key it works (for example):
<form method="POST" action="/search/the/5">
first put number 5 in text field and press enter it works!
But I want a dynamic way for this!
My main purpose of this is to convert this URL
http://localhost:8000/search/the/?find=5
to this
http://localhost:8000/search/the/5
Normally laravel using url query like http://localhost:8000/search/the/?find=5. But if you want to get url like http://localhost:8000/search/the/5. You should use javascript to change form action url instead. because you want to change url by input text on text field.
You set names as named route in routes.php. So you will need to replace this
<form method="POST" action="/search/the/5">
with
<form method="POST" action="{{route('/search/the', ['names' => 5])}}">
with variable
<form method="POST" action="{{route('/search/the', ['names' => $request->find])}}">