I would like to create a search system on Laravel 5.6.
My real problem is the function "search" to create in my Controller...
I have a table named clubs and I want to get the name_club only.
In my index.blade.php I have this
<form action="/search" method="get">
<div class="form-group">
<input type="text" name="search" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Search</button>
</span>
</div>
</form>
In my ClubController I have this
public function index()
{
$clubs = Club::oldest()->paginate(5);
return view('admin.index', compact('clubs'))
->with('i', (request()->input('page', 1)-1)*5);
}
For my function search() I don't know the syntax... I don't really understand how to integrate my pagination and the "name_club" field in my search function
public function search(Request $request)
{
$search = $request->get('search');
}
Route
Route::resource('/admin', 'ClubController');
Route::resource('/search','ClubController#search');
Thank you for your help.
You can paginate any regular query:
$search = $request->get('search');
$clubs = Club::oldest()
->where('name_club', 'like', "%$search%")
->paginate(5);
For more advanced searches I would recommend using Laravel Scout. Then it becomes something like this:
$search = $request->get('search');
$clubs = Club::search($search)->paginate(5);
Related
I am trying to search through my posts but clearly something is wrong since i am getting no results whether the query contains data present in my posts or not.
here is my blade file and search function
<h6 class="sidebar-title">Search</h6>
<form class="input-group" action="{{route('welcome-page')}}" method="GET">
<input type="search" class="form-control" name="search" placeholder="Search">
<div class="input-group-addon">
<span class="input-group-text"><i class="ti-search"></i></span>
</div>
</form>
search function
class WelcomeController extends Controller
{
public function index(){
$search = request()->query('search');
if ($search){
$posts = Post::where('title', 'LIKE', '%' .$search. '%')->paginate(1);
}else
{
$posts = Post::paginate(2);
}
return view('welcome')->with('categories', Category::all())
->with('tags', Tag::all())
->with('posts', Post::paginate(2));
}
I was trying to search the get the correct results
Sorry for my grammar, english is not my native language.
My data table is working fine when is not doing any search. But, when I do a search with the field nombre is giving me this error:
Undefined property: stdClass::$departamento (View: /Users/xxx/Sites/contablerd/resources/views/rrhh/empleado/index.blade.php)
This is my "Advanced Search" above the table:
<form action="#" method="GET">
<h3>Busqueda Avanzada</h3><br>
<div class="row">
<div class="row g-2">
<div class="col-md">
<div class="form-floating">
<input type="nombre" class="form-control" id="nombre" name="nombre" placeholder="" value="">
<label for="nombre">Nombre</label>
</div>
</div>
</div>
</div><br>
<input type="submit" value="Search" class="btn btn-secondary">
</form>
This is the part of the code where I am using a foreach. This part work fine when I am not using the "Advanced Search" (rrhh/empleado/index.blade.php):
#foreach($empleados as $empleado)
<tr>
<td>{{$empleado->id}}</td>
<td>{{$empleado->nombre}}</td>
<td>{{$empleado->apellido}}</td>
<td>{{$empleado->departamento->det}}</td>
<td>{{$empleado->cargo->det}}</td>
<td>{{$empleado->nomina->det}}</td>
<td>{{number_format($empleado->sueldo,2)}}</td>
<td>{{number_format($empleado->ars_empleado+$empleado->afp_empleado,2)}}</td>
<td>{{number_format($empleado->sueldo-($empleado->ars_empleado-$empleado->afp_empleado),2)}}</td>
<td>
<a class="btn btn-info" href="/empleado/{{$empleado->id}}/edit">Editar</a>
<button class="btn btn-danger">Borrar</button>
</td>
</tr>
#endforeach
This is my index method where I confirm is there is any request to change the query (EmpleadoController.php):
public function index(Request $request)
{
$empleados = \DB::table('empleados');
if($request->input('nombre')){
$empleados = $empleados->where('nombre', 'LIKE', "%".request('nombre')."%");
$empleados = $empleados->get();
}else{
$empleados = Empleado::all();
}
return view('rrhh.empleado.index',
[
'empleados'=>$empleados
]);
}
So when I do a search is giving me the above error. I got all my relationships in Empleado.php model.
I would like to ask too, is this the correct way to do this?
Thanks in advance and happy rest of the day.
Added eager loading for departamento, cargo and nomina relations and used the query based on the model, using DB will not access your relations.
public function index(Request $request)
{
if($request->has('nombre')) {
$empleados = Empleado::with(['departamento', 'cargo', 'nomina'])->where('nombre', 'LIKE', '%' . $request->get('nombre') . '%')->get();
} else {
$empleados = Empleado::all();
}
return view('rrhh.empleado.index', ['empleados' => $empleados]);
}
First, Did you identify the code line where this error was generated Undefined property: stdClass::$departamento ?
Second, it's recommended use compact instead array in the controller, like that
public function index(Request $request)
{
$empleados = \DB::table('empleados');
if($request->input('nombre')){
$empleados = $empleados->where('nombre', 'LIKE', "%".request('nombre')."%");
$empleados = $empleados->get();
}else{
$empleados = Empleado::all();
}
return view('rrhh.empleado.index',compact('empleados'));
}
If there are something wrong in your model, you need share it here to can see how you have the relationships
My search functionality uses laravel. Whenever I'd search something, instead of returning the wanted result, it returns all the data within the database. For eg: if I searched 'a', it will return all data from the database.
My Search form is
<form class="form-inline my-2 my-md-0" action="/search" method="GET">
<input class="form-control" type="text" placeholder="Search">
</form>
Search.blade.php
#section('content')
#if($search->isNotEmpty())
#foreach ($search as $searched)
<div class="post-list">
<p>{{ $searched->filename }}</p>
<p>{{ $searched->albumname }}</p>
<p>{{ $searched->artistname }}</p>
#endforeach
#else
<div>
<h3>No songs/artist/albums found</h3>
</div>
#endif
#endsection
Search routing
Route::get('/search', [UploadController::class,'search']);
UploadController.php
public function search(Request $request){
$searching = $request->input('search');
// $posts = DB::select('select * from users where active = ?', [1])
$search = MusicUpload::query()
->where('filename','LIKE',"%{$searching}%")
->orWhere('artistname','LIKE',"%{$searching}%")
->orWhere('albumname','LIKE',"%{$searching}%")
->get();
return view('pages.search',compact('search'));
}
I think there is problem with the upload query in the controller, but I haven't found out the exact place where the problem occurs.
You must out or queries in callback - call-back here like a braces in sql query.
Or you can put this query to scope:
class MusicUpload extends Model
{
public function scopeSearch($query, $search)
{
return $query->where(function($query){
$query->where('filename','LIKE',"%{$searching}%")
->orWhere('artistname','LIKE',"%{$searching}%")
->orWhere('albumname','LIKE',"%{$searching}%");
});
}
}
$searching = $request->input('search');
$search = MusicUpload::search($searching)->get();
return view('pages.search',compact('search'));
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.
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.