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
Related
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
im trying to search using three inputs, in the first scenario when i search using all the inputs it return the result, in the second scenrio when i search by using only one field it return all the records what im trying to do is when i search with one field it return a specsific result,
public function multiSearch(Request $request){
$search = $request->input('search');
$search_1 = $request->input('search_1');
$search_2 = $request->input('search_2');
$cars = Car::query()
->where('price', 'LIKE', "%{$search}%")
->orWhere('model', 'LIKE', "%{$search_1}%")
->orWhere('transmission', 'LIKE', "%{$search_2}%")
->get();
return view('car.car-multisearch', ['cars'=>$cars]);
}
this is my blade code
<form type="get" action="{{url('/search-car')}}">
#csrf
<div class="row">
<div class="col-md-4">
<p>
<input type="text" name="search" placeholder="Price" />
</p>
</div>
<div class="col-md-4">
<p>
<input type="text" name="search_1" placeholder="Model Year" />
</p>
</div>
<div class="col-md-4">
<p>
<select name="search_2">
<option data-display="Transmission">Transmission</option>
<option value="Automatic">Automatic</option>
<option value="Manual">Manual</option>
</select>
</p>
</div>
<button type="submit" class="gauto-theme-btn">Find Car</button>
</div>
</form>
You can dynamically add where statements to your query depending on values presence in the request and if they are not empty. Example:
public function multiSearch(Request $request){
$cars = Car::query();
if ( $request->filled('search') ) {
$cars->where('price', 'LIKE', "%{$request->input('search')}%");
}
if ( $request->filled('search_1') ) {
$cars->orWhere('model', 'LIKE', "%{$request->input('search_1')}%");
}
if ( $request->filled('search_2') ) {
$cars->orWhere('transmission', 'LIKE', "%{$request->input('search_2')}%");
}
return view('car.car-multisearch', ['cars'=> $cars->get() ]);
}
This solution do the job, but looks not good because you will need to add more if statements for each filter.)
i have a question about the Laravel search function, i had follow the guildeline online and i still fail to search the category, can someone guide me and tell me where i did wrongly ? Much appreciated
My category Controller php code:
public function search(Request $request)
{
$search = $request->get('search');
$posts = DB::table('bit_app_policy_category')->where('id','like','%' .$search. '%')->paginate(5);
return view('category.index',['posts' => $posts]);
}
My index.blade code
<div align="left">
<div class="col-md-4">
<h1>Policy</h1>
</div>
<div class="col-md-4">
<form action="/search" method="get" role="search">
{{ csrf_field() }}
<div class="input-group">
<input type="text" class="form-control" name="_method" placeholder="Search ID / Code"> <span class="input-group-btn">
<button type="submit" class="btn btn-primary">Search</button></span>
</div>
</form>
</div>
</div>
web.php
Route::get('/search','categoryController#search');
What error i get is here
Error image
interface
Database
You are sending $posts variable to your view. But the error says you are referencing a $category variable.
return view('category.index',['posts' => $posts]);
Maybe you might want to update view to use $posts. If you could post your full code (category/index.blade.php) we might be able to help you better.
__
Here is how I would do:
$categories= DB::table('bit_app_policy_category')->where('id','like','%' .$search. '%')->paginate(5);
return view('category.index',['categories' => $categories]); //you can also use compact return view('category.index', compact('categories') );
And to display:
#foreach( $categories as $category )
<div>{{ $category->id }}</div>
#endforeach
Another tip: you can name your routes like so
Route::get('search','categoryController#search')->name('search');
Then you can reference this route (in form or anywhere else you want) like so:
<form action="{{ route('search') }}" ..>
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);
I'm a laravel newbie!!! And struggling to find out how to create a search and return the results.
Here's all my code on gist https://gist.github.com/anonymous/8289692
I've put everything in the gist from the form to the route, the controller and the model!
/* Route */
Route::get('/search/{q}', 'HomeController#search');
/* Form from web page */
<form class="navbar-form navbar-left" role="search" action="/search/" method="post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search Programmes" name="1">
<span class="input-group-btn">
<input type="submit" class="btn btn-primary" value="Search">
</span>
</div><!-- /input-group -->
</form>
/* Controller */
public function search($q)
{
$q = Input::get('term');
$searchTerms = explode(' ', $q);
$query = DB::tables('wc_program');
foreach($searchTerms as $term)
{
$query->where('JobRef', 'LIKE', '%'. $term .'%');
}
$results = $query->get();
}
/* Model (just in case) */
class Search extends Eloquent {
protected $table = 'wc_program';
public $timestamps = false;
}
There are some issues in your code:
Your form is using POST method, but your route is a GET route. And this accounts for the NotFoundHttpException. So make your route like:
Route::post('search', 'HomeController#search');
There's no need for a parameter, since your catching it with POST anyway, not GET!
Your input doesn't have the name 'term', but '1'. That might be a typo, but anyway, make it so:
<input type="text" class="form-control" placeholder="Search Programmes" name="term">
Also, I suggest using Laravel's URL methods to build a correct url:
<form class="navbar-form navbar-left" role="search" action="{{URL::to('search')}}" method="post">
Or better:
{{Form::open(array('url' => 'search', 'class' => 'navbar-form navbar-left', 'role' => 'search')}}
Now, to the controller. Let's rewrite to suite the new route:
public function search() //no parameter now
{
$q = Input::get('term');
if($q && $q != ''){
$searchTerms = explode(' ', $q);
$query = DB::table('wc_program'); // it's DB::table(), not DB::tables
if(!empty($searchTerms)){
foreach($searchTerms as $term) {
$query->where('JobRef', 'LIKE', '%'. $term .'%');
}
}
$results = $query->get();
dd($results); // for debugging purpose. Use a View here
}
}