Laravel search facility doesn't give output - php

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();
}

Related

Laravel Searching returns all data instead of wanted data

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'));

Pagination includes _token string

I'm adding pagination to my view but for some reason the URL string includes a "_token" string. How can I remove that?
Here is How my controller looks like:
public function index($client_id, Request $request)
{
$client = Client::where('id', $client_id)->first();
if ($request->search) {
$extensions = Extension::query()
->where('first_name', 'like', '%'. $request->search .'%')
->where('client_id', 'like', '%'. $client_id .'%')
->paginate(5);
}else{
$extensions = Extension::where('client_id', $client_id)->paginate(5)->withQueryString();
}
return view("pages.extensions.index", compact('client', 'extensions'));
}
My index blade has this:
<!-- Top Bar -->
<div>
<form action="{{ route('extensions.index', [$client->id]) }}" method="GET">
#csrf
<x-input type="text" name="search" id="search" placeholder="Search by First Name..." />
<button type="submit">Search</button>
</form>
</div>
<!-- Table -->
// A table goes here
<!-- Pagination Link -->
<div>
{{ $extensions->links() }}
</div>
Here is how looks like the URL after I use the search:
http://127.0.0.1:8001/clients/1/extensions?_token=Cdri3a6GEEOe90I9niEmVI&search=frodo
Try removing #csrf from your form.
As your form is doing GET request the _token is not needed at all.

Controller - Search System Laravel 5.6

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);

Search bar in Laravel 5.3 to filter the table

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.

Laravel - Search Facility on site using Eloquent

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
}
}

Categories