Trying to set up basic search functionality for products. I am having trouble sorting the route parameter variable and passing the query string to the search function.
Route::get('/search/{query?}', 'ProductController#searchable');
This works and returns a query when I input the query manually.
Controller
public function searchable($query)
{
// search database, with result, list on page, with links to products,
$products = Product::search($query)->get();
return view('search.index', compact('products'));
}
However, I would like it to come from the URL /search?test.
My form shows:
{{ Form::open(array('action' => 'ProductController#searchable', 'method' => 'get', 'files' => 'false')) }}
<input type="search" name="search" placeholder="type keyword(s) here" />
<button type="submit" class="btn btn-primary">Search</button>
{{ Form::close() }}`
I am new to Laravel and need a little help. I am using Laravel Scout and TNTSearch.
You don't need to user {wildcard} for searching. We have Request for that
Route::get('search', 'ProductController#searchable');
Pass the url instead.
{{ Form::open(array('url' => 'search', 'method' => 'GET', 'files' => 'false')) }}
<input type="search" name="search" placeholder="type keyword(s) here" />
<button type="submit" class="btn btn-primary">Search</button>
{{ Form::close() }}
In Controller simple fetch $request->search
public function searchable(Request $request)
{
// search database, with result, list on page, with links to products,
$products = Product::search($request->search)->get();
return view('search.index', compact('products'));
}
Related
I'm very confused, I need your help, this is the error:
Missing required parameter for [Route: single.temp] [URI: singlepost/{name}] [Missing parameter: name]. (View: C:\Users\Toshiba\Desktop\working\mouhawla\resources\views\index.blade.php)
The search field:
<div class="search">
<form role="form" action="{{route('single.temp')}}">
<i class="fa fa-search"></i>
<div class="field-toggle">
<input type="text" name="name" class="search-form" autocomplete="off" placeholder="Search">
</div>
</form>
</div>
The method:
public function getPostByName($name) {
$products = DB::table('templates')
->where('name', $name)
->first();
return view('singlepost', compact('products'));
}
The route:
Route::get('/singlepost/{name}', 'App\http\controllers\TemplatesController#getPostByName')->name('single.temp');
The final view:
<h1 style="text-align: center;">ACH-template</h1>
<table>
<tr>
<td><img src="/storage/{{$products->image_path}}"></td>
<td><img src="/storage/{{$products->image_path2}}"></td>
<td><img src="/storage/{{$products->image_path3}}"></td>
<td>
<p>
<h2 style="text-align:center;">{{$products->name}}</h2>
</br>
<p>{{$products->description}}</p>
</p>
</td>
</tr>
</table>
<a href="/storage/{{$products->file_path}}" class="btn btn-primary">
<button class="btn" style="width:100%">
<i class="fa fa-download"></i> Download
</button>
</a>
The error is very explanatory. You are trying to use /singlepost/{name} route, but on your blade file, you are doing route('single.temp'), it is telling you that it needs the parameter name, else it cannot create the URL as it is a missing parameter.
You should have something like:
<form role="form" action="{{route('single.temp', ['name' => VALUE'])}}">
But that will not solve your problem, as you are trying to do a search, so you want something like /singlepost/John, and John is going to be input by the user on the input field. So you have to do an AJAX call because {{ route('single.temp') }} is going to be rendered by PHP and served to the user, so it is always going to miss the needed parameter.
What you can also do is get that value from the Request instead of a URL parameter.
You have defined a route which requires a parameter: {$name}. You have also used the route helper to generate a URL which takes the name of a route as the first argument and an array of parameters as an optional second argument.
When you have used route('single.temp') in your form action, you have not specified any parameters and so Laravel is throwing the error you're seeing. To resolve this error, you would need to specify a $name parameter as the second argument (i.e. route('single.temp', ['name' => 'something'])). This is not ideal though as if you're using $name as a search term, you don't know the value when the page is first rendered and so can't provide that value.
There are a few ways you could achieve your goal of searching records, a basic example of how you could do this follows.
web.php
Define two routes, the first to return a view with a form and another to process the form submission and show the results.
Route::get('/templates', [TemplateController::class, 'index'])
->name('templates.index');
Route::get('/templates/search', [TemplateController::class, 'search')
->name('templates.search');
TemplateController.php
Define the two functions which will be used when one of the routes defined above is requested.
class TemplateController extends Controller
{
// return a view
public function index()
{
return view('templates.search', ['templates' => []]);
}
// process the form submission
// perform a search for the $request search term
// return a view with the results
public function search(Request $request)
{
$request->validate([
'term' => ['required', 'string']
]);
$templates = Template::where('name', $request->term)->get();
return view('templates.search', ['templates' => $templates]);
}
}
templates/search.blade.php
{{-- create a form which will submit to the search route --}}
{{-- note I use GET rather than POST here, explained later --}}
<form action="{{ route('templates.search') }}" method="GET">
#csrf
<input type="text" id="term" name="term" />
<button type="submit">
{{ __('Search') }}
</button>
{{-- loop over and show results if there are any --}}
#forelse ($templates as $template)
{{ $template->name }}
#empty
{{ __('Empty') }}
#endforelse
</form>
The above should be self explanatory. My reason for using GET rather than POST in the search is because the value will be added to the URL as a query string parameter meaning it can be bookmarked or shared with ease.
I have a problem to store data in a table. And after hours I still couldnt figure out, what the problem is. I would be so happy for some help!
WishlistController.php:
public function store($book_id)
{
$user_id=Auth::id();
$wishlist=new Wishlist;
$wishlist->book_id=$book_id;
$wishlist->user_id= $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id->id)
->with('success', 'Buch gewünscht');
The Model:
class Wishlist extends Model
{
public $table = 'wishlist';
public $fillable = ['book_id','user_id',];
the view.blade:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
<form action="someaction" method="POST">
<input type="hidden" name="book_id" value="{{$book->id}}"/>
</form>
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD.store', 'WishlistController#store');
When I check the table, nothing new is added.
Its frustrating :-(
Just change post to get in this line:
Route::post('wishlistCRUD.store', 'wishlistCRUD#store');
Try to do this. In your Controller, you don't need $book_id as a parameter because you can get it from the $request:
public function store(Request $request)
{
$user_id = Auth::id();
$book_id = $request->book_id;
$wishlist = new Wishlist;
$wishlist->book_id = $book_id;
$wishlist->user_id = $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id)
->with('success', 'Buch gewünscht');
}
Don't forget to add use Illuminate\Http\Request;
And then in your blade, you don't need to set a route in an anchor, because it is already defined in the form. And you don't need two forms, because Form::open already does that:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
{{ csrf_field() }}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
{!! Form::submit('wünschen') !!}
{!! Form::close() !!}
This should work.
You have created POST method in your web.php for adding book to wish list, in your blade template you need to submit hidden form on click of link.
in your web.php
Route::get('wishlistCRUD/book/{book_id}','WishlistController#get_book_by_id')->name('get_book_by_id');
Route::post('wishlistCRUD.store', 'WishlistController#store')->name('store');
In your blade template
<a class="btn btn-primary" href="javascript:void(0)" onclick="event.preventDefault();document.getElementById('addToWishlist').submit();">wünschen</a>
<form style="display:none" id="addToWishlist" method="POST" action="{{ route('store')}}">{{csrf_field()}} <input type="hidden" name="bookid" value="{{$book->id}}" /> </form>
in your controller
public function store(Request $request){
$bookID= $request->input('bookid');
$wishlist=new Wishlist();
$wishlist->book_id= $bookID
$wishlist->user_id= Auth::user()->id;
$wishlist->save();
return redirect()->route('get_book_by_id', ['book_id' => $bookID]);
}
public function get_book_by_id(Request $request,$book_id){
// find book by ID;
$book=Book::find($book_id);
// book found
if($book){
return view('book')->with('book',$book);
}else{
// book not found , redirect to 404 page or home page
return redirect('/');
}
}
In blade, change the
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
to a <button class="btn btn-primary" type="submit">wünschen</button>
Delete the tag form, it's not necesary. You declared it with the {!! form:open... !!}
You are trying to access to route('wishlistCRUD.store',$book->id) by GET with the <a></a>
Try this:
the view.blade:
{!! Form::open(['url' => 'wishlistCRUD/store', 'method' => 'POST']) !!}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
<button class="btn btn-primary" type="submit">wünschen</button>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD/store', 'WishlistController#store');
Currently trying to paginate some results on a product object.
The paginate object that I am receiving in my view {{ $products->links() }} is returning correctly however instead of the query being "test.com/search?search=test&page2" it is returning "test.com/search?query=test&page2" how can i change it so it returns search instead of query?
The html output is:
2
but what i need is:
2
EDIT:
Controller:
public function searchable(Request $request)
{
// search database, with result, list on page, with links to products,
$products = Product::search($request->search)->paginate(3);
return view('search.index', compact('products', 'request'));
}
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Product extends Model
{
use Searchable;
/**
* Creates searchable index for product model
* #return [type] [description]
*/
public function searchableAs()
{
return 'products_index';
}
}
form:
{{ Form::open(array('url' => '/search', 'method' => 'GET', 'files' => 'false')) }}
<div class="row">
<div class="col-md-2">
<button type="submit" class="custom-search-button btn btn-lrg btn-primary">Search</button>
</div>
<div class="col-md-10">
<input class="search-bar" type="search" name="search" placeholder="type keyword(s) here" />
</div>
</div>
{{-- <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> --}}
{{ Form::close() }}
You have field named query in your search form. Change its name to search, adjust logic in controller to be using search param instead of query and you are done.
Missing argument 2 for App\Http\Controllers\Company\OrderController::store()
I got this error in my store controller, my form is passing 2 parameters but the second one is not found.
Route: Route::resource('order', 'OrderController');
$company gets converted to a model in the controller.
The form:
<form class="form-horizontal" role="form" method="POST" action="{{action('Company\OrderController#store', [$company,$orderid])}}">
{{ csrf_field() }}
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Any ideas?
Thanks!
If you created store route with Route::resource() it doesn't expect any parameters and should look like this:
public function store(Request $request)
So, you need to pass data using hidden inputs, like:
{!! Form::hidden('data', 'some data') !!}
And then get data in controller with:
$data = $request->data;
You should specify the key-value pair like this:
['company_id' => $company->id, 'order_id' => $order->id]
So your form would look like:
<form action="{{ action('Company\OrderController#store', ['company_id' => $company->id, 'order_id' => $order->id]) }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary">Accept</button>
</form>
Hope this helps!
I have a problem, I want to use the form to do a search and use pagination. The problem is when I change the page, an error occurs:
No query results for model [Aluno].
My controller
Route::post(
'alunos/busca',
array(
'as' => 'alunos.busca',
'uses' => 'AlunosController#busca'
)
);
My Controller
public function busca()
{
$search = Input::get('q');
$alunos = Aluno::where('curso', $search)->paginate(1);
return View::make('alunos.index', compact('alunos', 'search'));
}
My view:
<form method="post" action="{{ URL::to('alunos/busca') }}">
<input class="input-xxlarge" name="q" type="text" placeholder="Search...">
<div class="control-group">
<div class="controls">
<input type="submit" class="btn" id="submit" value="Submit" />
</div>
</div>
<div class="divPagination">
{{ $alunos->links() }}
</div>
I need help..
Sorry my English
I think the best solution is the next one:
Copy and paste this form wherever you want it:
{{Form::open(array('url' => 'alunos/busca','method' => 'get'))}}
{{Form::text('q', null , array('placeholder' => 'Search...','class' => 'input-xxlarge'))}}
{{Form::submit('SEARCH',array('class' => 'btn','id' => 'submit'))}}
{{Form::close()}}
In routes.php :
Route::get('alunos/busca', function() {
$search = Input::get('q');
$alunos = Aluno::where('curso', 'LIKE', '%'.$search.'%')->paginate(15);
return View::make('alunos.index')
->with('alunos', $alunos);
});
Now, in your view app/views/alunos/index.blade.php :
#if(!$alunos->isEmpty())
#foreach($alunos as $aluno)
{{$aluno->subject}}
#endforeach
{{$alunos->links()}}
#else
<h1> No results </h1>
#endif
Change it as you wish and enjoy
The problem is when you click on the next page it has no memory of the search param you just entered, essentially $q = null; which will cause the error.