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
}
}
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
I am trying to create a searching system with pagination in Laravel that displays the results refined by multiple options from an inquiry list.
The refined search works fine, but when I click the pagination link, the all options are reset.
First, I was trying to store sessions but it didn't work somehow.
After I posted this question, I changed my code as below.
Currently, I'm trying to use "attends" but still it doesn't work.
The options are reset when I click the pagination link.
** Codes below are edited after posted this question. **
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Contact;
class ContactController extends Controller
{
public function index()
{
$contacts = Contact::paginate(10);
return view('admin/index', ['contacts' => $contacts]);
}
public function search(Request $request)
{
$query = Contact::query();
$fullname = $request->input('fullname');
$gender = $request->input('gender');
$email = $request->input('email');
$created_at_start = $request->input('created_at_start');
$created_at_end = $request->input('created_at_end');
if ($fullname != '') {
$query->where('fullname', 'like', '%'.$fullname.'%');
}
/* Other IF options */
else {
$contacts = Contact::paginate(10);
}
$contacts = $query->paginate(10);
return view('admin/index', compact('contacts'));
}
}
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ContactController;
Route::get('/', function () {
return view('index');
});
Route::get('admin', [ContactController::class, 'index']);
Route::get('admin.search', [ContactController::class, 'search'])->name('admin.search');
SearchForm:
<form class="search-box__items" action="admin" method="GET">
#csrf
<label class="search-box__items__label">
Name
<input name="fullname" type="text" class="search-box__item" value="{{ $fullname ?? '' }}">
</label>
<!-- Other options -->
<input type="submit" value="Search">
</form>
<div class="contacts">
<div class="pagination">
{{ $contacts->appends(['fullname'=>$fullname ?? ''])->links() }}
</div>
<div class="contacts-table">
<!-- Inquiry list -->
</div>
</div>
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 am doing a search facility function that get's a value from a textbox (it does that I have checked) and compares to 'skill' column in 'skills' table. If there is a match either one character or whole word It should output details from 'handymen' table, these tables are linked with 'hasMany' and 'belongsToMany' in models.
However the issue is that I do get output, but it's a lit of all handymen even if I type an empty string into textbox.
Controller:
function search()
{
$skills = Skill::all();
return view('layouts/search',['skills' => $skills]);
}
//$searchTerm = request('skill');
function details() {
$handymen = Handyman::whereHas('skills', function($query) {
if(!empty($searchTerms)){
foreach($searchTerms as $skill) {
$query->where('skill', 'LIKE', '%'. $skill .'%');
}
}
})->get();
return view('layouts/details', compact('handymen'));
}
View-search:
#extends('layouts.master')
#section('title', 'Search Page')
#section('content')
<h1>Here you can search</h1>
<form action="{{url('details')}}" method="POST">
{{ csrf_field() }}
<div>
<input type='text' name='skill' />
</div>
<input type="submit" name="submitBtn" value="Search">
</form>
#endsection
View-details-here should be my results:
#extends('layouts.master')
#section('title', 'Add Job')
#section('content')
<h1>Handyman details</h1>
<ul>
#foreach ($handymen as $handyman)
<a href= "{{url('skilledHandyman/'.$handyman->id)}}">
{{$handyman->first_name}}</a>
#endforeach
</ul>
#endsection
Where does the $searchTerms variable get into the details() function?
Did you omit some code?
I'm asuming it is set in the details function somehow, if that is the case, then you don't pass $searchTerms to the scope of the whereHas closure.
Try adding it to the use part of the closure, like below:
function details() {
// .. somehow get $searchTerms first here.
$handymen = Handyman::whereHas('skills', function($query) use($searchTerms) {
if(!empty($searchTerms)){
foreach($searchTerms as $skill) {
$query->where('skill', 'LIKE', '%'. $skill .'%');
}
}
})->get();
}