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])}}">
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
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.
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 am trying to connect my search form to my database and retrieving data from there. But something is going wrong. My controller or my routes... But I couldn't figure it out.
the problem is it's showing the data directly without, searched.
I want to see the data when I search. Also try to search and this is coming:
Collection {#221 ▼
#items: []
}
My view is here:
<form action="{{URL::to('welcome')}}" method="post" role="search" class="searchbox">
{{csrf_field()}}
<input type="text" name="q" class="search" placeholder="町, 地域, 会社名, 物件名">
<input type="submit" name="submit" class="submit" value="search">
</form>
#if(isset($details))
<p> here is the results <b>{{$query}}</b> are : </p>
#endif
<table cellspacing='0'>
<thead>
<tr>
<th>会社名</th>
<th>物件名</th>
<th>住所</th>
<th>販売価格</th>
<th>専有面積</th>
<th>間取り</th>
<th>竣工時期</th>
<th>入居時期</th>
</tr>
<thead>
<tbody>
#foreach($estates as $estate)
<tr class="even">
<td>{{$estate->company_name}}</td>
<td>{{$estate->name}}<br/></td>
<td>{{$estate->address}}</td>
<td>{{$estate->price}}</td>
<td>{{$estate->extend}}</td>
<td>{{$estate->rooms}}</td>
<td>{{$estate->old}}</td>
<td>{{$estate->entery}}</td>
</tr>
#endforeach
</tbody>
</table>
Also here is the controller and my route.
What am I doing wring here?
public function welcome()
{
$estates = Estates::orderBy('price')->get();
$data['estates'] = $estates;
return view('welcome', $data);
}
public function search(Request $request)
{
$q = $request->q;
if ($q != " "){
$estates = \DB::table('estates')->where("name","LIKE", "%" . $q . "%")
->orWhere("address","LIKE", "%" . $q . "%")
->get();
dd($estates);
if(count($estates) > 0){
return view("welcome", compact('estates'))->withQuery($q);
}
}
return view("welcome")->withMessage("No Found!");
}
Here is route:
Route::get("/", "PagesController#welcome");
Route::post("/", "PagesController#search")->name('search.route');
I couldn't figure it out. Also first controller welcome retrieving data smoothly without search form! But I want it to run when I try to search. Any help? Thank you!
Change your form action to
<form action="{{ route('search.route') }}" method="post" role="search" class="searchbox">
Change your route to
Route::post("/", "PagesController#search")->name('search.route');
Add use Illuminate\Http\Request; on top of your PagesController and change your controller method to
public function search(Request $request)
{
$q = $request->q;
if ($q != " "){
$estates = \DB::table('estates')->where("name","LIKE", "%" . $q . "%")
->orWhere("address","LIKE", "%" . $q . "%")
->get();
dd($estates);
if(count($estates) > 0){
return view("welcome", compact('estates'))->withQuery($q);
}
}
return view("welcome")->withMessage("No Found!");
}
here you can do it this way too
change the route like this
Route::post("/", "PagesController#search")->name('routeName');
and change the form action like this
<form action="{{route('routeName')}}" method="post" role="search" class="searchbox">
or edit you code and pass the url to value in quotes
<form action="{{URL::to('welcome')}}" method="post" role="search" class="searchbox">
For more information check this Laravel Routes
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
}
}