Laravel search field - laravel

I have some issues with search field in my project. Hopefully someone can help me.
When I type what I want to search and press enter in url i just get
sitelink/?
instead of
sitelink/results?query=someting
Here are parts of code
All search logic is in web.php not in a seperate controller...
web.php
Route::get('/results', function(){
$posts = \App\Post::where('title','like', '%' . request('query') . '%')->get();
return view('results')->with('posts', $posts)
->with('title', 'Search results : ' . request('query'))
->with('settings', \App\Setting::first())
->with('categories', \App\Category::take(6)->get())
->with('query', request('query'));
});
Here is form
<form method="GET" action="/results">
<input class="overlay_search-input" name="query" placeholder="Type and hit Enter..." type="text">
<a href="#" class="overlay_search-close">
<span></span>
<span></span>
</a>
</form>
Also, when I type full url manualy for example
sitelink/results?query=find
It is working just fine but it does not send right url when I press enter on search field

Are you sure you are hitting your route ???
Try to get the query param in your function
Route::get('/results', function(){
$query = Input::get('query');
print_r($query);
$posts = \App\Post::where('title','like', '%' . $query . '%')->get();
});

The issue was in mistype in blade.php
I hope this will help someone.
Essentially, I had a form I header for search but I also had form in frontIndex.blade.php in one was edited form how it should be and in one it was not. So I created includes folder cutter form there and then included where I needed it. Now it is working just fine :)

Replace this one:
<form method="GET" action="action="{{ route('result') }}"">
<input class="overlay_search-input" type="search" value="{{ isset($query) ? $query : '' }}"
name="query" placeholder="Type and hit Enter..." type="text">
<a href="#" class="overlay_search-close">
<span></span>
<span></span>
</a>
</form>
web.php:
Route::get('/result','SearchController#search')->name('result');
Your controller should get the query input:
$query = $request->input('query');

Related

Error message "The GET method is not supported for this route. Supported methods: POST." in laravel 8

Following is my controller. Method ProjectsView is to view all the listings. The second method BackendProjectSearch is for searching of projects. The first page of search result is displayed properly, but when we click on next page it gives the error "The GET method is not supported for this route. Supported methods: POST."
What should I do ?
public function ProjectsView(){
$projects = projects::orderBy('id','ASC')->paginate(15);
return view('backend.projects.projects_view',compact('projects')); }
public function BackendProjectSearch(Request $request){
$request->validate(["search" => "required"]);
$item = $request->search;
$projects = Projects::where('project_name','LIKE',"%$item%")->paginate(15);
return view('backend.projects.projects_view',compact('projects')); }
Following are the routes for both the methods :
Route::post('/backend/project/search', [ProjectsController::class, 'BackendProjectSearch'])->name('backend.project.search');
Route::get('/view', [ProjectsController::class, 'projectsView'])->name('projects.view');
View code :
<div class="col-md-8">
<div class="header-navsearch">
<form class="form-inline mr-auto" method="post" action="{{route('backend.project.search')}}">
#csrf
<div class="nav-search">
<input type="search" name="search" class="form-control header-search" placeholder="Search projects…" aria-label="Search">
<button class="btn btn-primary" type="submit"><i class="fa fa-search"></i></button>
</div>
</form>
</div>
</div>
The route for this request should be post, not get.
Example
Route::post(-----);
Also, make sure to insert a CSRF token while making the request
it means you now cannot do something like the following.
Instead, you have to do something like...
<form action="{{ route('example') }}" method="POST">
#csrf {{-- According to the version of laravel --}}
{{-- Your other codes --}}
<button type="submit" class="">Submit</button>
</form>
You have to use the below code
return redirect()->route('projects.view')->with(['projects' => $projects]);
Your route might be little change
Route::get('/view/{projects?}', [ProjectsController::class, 'projectsView'])->name('projects.view');
and In your controller, you have to change function like this
public function ProjectsView($projects = null){
if($projects!=null){
return view('backend.projects.projects_view',compact('projects'));
}
else{
$projects = projects::orderBy('id','ASC')->paginate(15);
return view('backend.projects.projects_view',compact('projects'));
}

Pagination with search query

Controller:
public function search(Request $request)
{
$stores = Store::with('router', 'tills', 'backOffices','contacts', 'storeStatus', 'storeStatus', 'storeType', 'scos', 'fuelBrand')->where('store_name', 'like', '%' . $request['find'] . '%')->paginate(13);
return view('store.index')->with([
'stores' => $stores,
'find' => $request['find'],
]);
}
View
<div class="flex-1 mx-10">
<form action="/store/search" method="POST" name="search" role="search">
#csrf
<input id="index-view-search" name="find" type="text" placeholder="Search"
class="border rounded w-full p-1"
>
</form>
</div>
<div class="mx-3">
#if ( Route::is('store.index') )
{{ $stores->links() }}
#else
{!! $stores->appends(['find' => $find])->links() !!}
#endif
</div>
Route
Route::any('/store/search', "StoreController#search")->name('stores.search');
Can anyone tell me why I get a 404 when I click the pagination links for this code?
The route accepts post or get, I pass back the search term and the page number... /store/search?find=evans&page=2 is what the pagination instance is requesting, I cannot see what is wrong
I actually figured it out, As I have a resource controller so store/{id} is active so I need to rename the URI.

HOw to make simple search with laravel

i try to create forms search in laravel..
when the title of the title article is searched .. then the title will appear.. i am search title article form Article Table
this is my HomeController
...
public function search(Request $request){
$cari = $request->get('search');
$Title = Article::where('title', 'LIKE', '%' .$cari . '%')->paginate(10);
return view('/article/show', $cari);
}
this is my header.blade.php
**...
<div class = "col-md-4">
{!! Form::open(['method'=>'GET', 'url'=>'/article/show', 'role'=>'search']) !!}
<div class= "input-group custom-search-form">
<input type="text" class="form-control" name="search" placeholder="Judul..">
<span class="input-group-btn">
<span class="input-group-btn">
<button class="btn-btn-default" type="submit"><i class="fa fa-search"></i>Cari</button>
</span>
</span>
{!! Form::close()!! }
</div>
</div>
thi is my route..
..
Route::get('/article/show', 'HomeController#search');
.
But when i am typing on search form.. i am getting error like this
(2/2) QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show AND is_show = TRUE and `articles`.`deleted_at` is null' at line 1 (SQL: select count(*) as aggregate from `articles` where category_id=show AND is_show = TRUE and `articles`.`deleted_at` is null)
.
please tell me which part is wrong
Thanks...
You're sending a POST request as GET. Switch the GET to POST, or include the searched term in the URL and remove the Request $request part of your search(Request $request) function since GET will not provide an instance of Request. Just make it search($terms) instead if you go that route. I would personally just switch to POST though.
IE:
Route::post('/article/show', 'HomeController#search');
{!! Form::open(['method'=>'POST', 'url'=>'/arti...
In your form add
{{ csrf_token() }}
Also you will want to check if 'search' has a value, so wrap it around a if ($request->search)
Also, it looks like you may have a SQL error somewhere else on the page which is cause this.
check your table name, or try this
public function search(Request $request)
{
$cari = $request->get('search');
$data['result']= DB::table('articles')->WHERE('title', 'LIKE', '%' .$cari . '%')->paginate(10);
return view('/article/show', $data);
}
your form
<form class="navbar-form navbar-left" method="GET" action="{{url('search')}}">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="search">
<button class="btn btn-default" type="submit">
<i class="fa fa-search"></i>
</button>
</div>
</form>

Codeigniter redirect design fail

So i have and action button of edit and the fields to be edited on the same page using php. my problem is when i reload the edit button it will come back to the same page and add an additional url data.
function view_branch()
{
$this->load->view('includes/a_header');
$this->profile();
$this->load->view('includes/a_footer');
$this->load->view('admin/content/view_branch');
}
on top is my original method which views my html page view_branch
on the bottom is the button that reloads the page go to controller and finds the edit_branch method
$row[] = '<td class="w3-row" >
<form method="post" class="w3-half"
action="edit_branch/'.$foo->BranchId.'" id="Edit">
<button class="btn btn-default">
<i class="fa fa-edit"></i>
</button>
</form>
</td>';
so this button will go to my controller with the method name edit_branch that holds
public function edit_branch()
{
$this->view_branch();
}
what will happen is that it creates another url that would cause and error
two url edit_branch
http://localhost/GFC/index.php/Admin_Dashboard/edit_branch/edit_branch/CEB1
You could construct the url differently using site_url(). Try this:
$row[] = '<td class="w3-row" >
<form method="post"
class="w3-half"
action="' . site_url('admin_dashboard/edit_branch/'.$foo->BranchId) . '"
id="Edit">
</form>
</td>';
Note: make sure the URL Helper is loaded before using the site_url() function

How can I pass my search keyword to my Controller?

I'm learning laravel 5.4 and i'm doing a sample project. In that I need to do a search from my DB. Below i have attached a screenshot of my interface.
As you can see in the searchbox i have given i need to search "malabe". And i need to get results for that. i have implemented Controller and needed views too. below i have attached my controller
class SearchController extends Controller
{
//search controller
public function search($keydown){
$searchDetails = DB::table('van_Services')
->where('vs_RouteTo','like','%'.$keydown.'%')
->orwhere('vs_RouteFrom','like','%'.$keydown.'%')
->orwhere('vs_Description','like','%'.$keydown.'%')
->orderBy('created_at', 'DESC')
->paginate(12);
return view('search.search', compact('searchDetails'));
}
}
route code
Route::get('/search/{key}','SearchController#search');
here is the div of my search box code,
<div class="col-xs-6 col-sm-2 col-md-2">
<div class="search_box pull-right">
{{--Search Button--}}
<input type="text" name="search" id="search" laceholder="Search"/>
</div>
</div>
My route and code works if i do the url manual,
my problem is how can i make my search box work? any tutorials or tips?
1. If you really need to use URL link.
JS solution is:
// q => Your search word
function redirectSearch(q) {
location.href = '/search/'+q;
}
pass search field value to this function and page redirect automatically.
.
2. You can send GET params to controller.
Front code:
<div class="col-xs-6 col-sm-2 col-md-2">
<div class="search_box pull-right">
<form action="/search" method="GET">
{{--Search Button--}}
<input type="text" name="search" id="search" placeholder="Search"/>
</form>
</div>
Controller code:
public function search(Request $request){
$search = $request->search;
... Your code ...
}
If you use method with request don`t forget about
use Illuminate\Http\Request;
Route:
Route::get('/search','SearchController#search');
My route and code works if i do the url manual,
I assume it's already working and redirection is just your problem.
Just simply use JS/jQuery.
// when ENTER is pressed
$(document).keypress(function (e) {
if (e.which == 13) {
e.preventDefault();
window.location = APP_URL + "/search/" + $('#search').val();
}
});
Goodluck.

Resources