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.
Related
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.
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') }}" ..>
So basically I'm doing a project for college and the only thing missing from my project is a working search form, but i don't know how to do it. All of my products are created and seeded into the database.
My routes:
Route::get('/', [
'uses' => 'ProductsController#getIndex',
'as'=> 'product.index'
] //tenho de meter um . porque nao é uma diretoria global, index ta dentro
duma pasta shop
);
my table:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string("imagePath");
$table->string("title");
$table->text("description");
$table->integer("price");
});
my form structure:
<form action="" method="post" class="navbar-form navbar-left " role = "search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
my products controller:
public function getIndex(){
$products = Products::all();
return view('shop.index', ['products' => $products]);
}
I've seen a lot of tutorials on how to do one but i cant seem to make it filter the ones i have, for example, if i have 3 boots and 5 shoes on the shop, if i write "boot" on the search, i want only for the 3 boots to appear
your HTML would be
<form action="{{ route('product.search') }}" method="post" class="navbar-form navbar-left " role = "search">
#csrf
<div class="form-group">
<input type="text" class="form-control" name="search" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
in your searchProduct function
for like query where it can search in title and if keyword present it will return all product who has same title you are searching for
public function searchProducts(Request $request){
$products = Product::where('title', 'like', '%'.$request->search.'%')->get();
return view('shop.index', ['products' => $products]);
}
for exact same title result
public function searchProducts(Request $request){
$products = Product::where('title',$request->search)->get();
return view('shop.index', ['products' => $products]);
}
there are a LOT of different ways to approach something like a search ... ranging from simple to very complex but a basic method would to search your title and description fields for a keyword like "boot".
You'll want to add a couple of things ...
First, add a new route to handle the search request:
Route::post('/results', [
'uses' => 'ProductsController#searchProducts',
'as'=>'product.search'
]);
Next, add a name to your html input, such as 'keyword' along with the blade shortcut for the CSRF token field:
<form action="/results" method="post" class="navbar-form navbar-left " role = "search">
#csrf
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" name="keyword">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
Then add a function to your controller to handle the query:
public function searchProducts(Request $request){
$results = Product::where('title', 'like', '%'.$request->search.'%')
->orWhere('description', 'like', '%'.$request->search.'%')
->orderBy('title', 'desc')
->get();
return view('search_results', ['results'=>$results, 'keyword'=>$request->keyword]);
}
Finally, create a new file named search_results.blade.php and place it in your views directory. This will be used to display the search results to the user.
<h2>Search Results for {{$keyword}}</h4>
<table>
<thead>
<tr>
<th>Product</th>
<th>Description</th>
<th>Price</td>
</tr>
</thead>
<tbody>
#forelse($results as $result)
<tr>
<td>{{$result->title}}</td>
<td>{{$result->description}}</td>
<td>{{$result->price}}</td>
</tr>
#empty
<tr>
<td colspan="3">
No Products Found
</td>
</tr>
#endforelse
</tbody>
</table>
With this foundation, you could really go wild and add filtering options, sorting, ranking and more. Hope that helps get you going!
you can do this
html
<form action="/search" method='post' class="navbar-form navbar-left " role = "search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" name="search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
PHP
public function search(Request $request)
{
$products = Product::where('title', 'LIKE', '%'.request->search.'%')->get();
return view('search', ['products' => $products]);
}
Only change wich view you want return and create too in to a routes/web.php.
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])}}">
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();
}