Reputation: 69
recently i've been learning lavavel, and i've come to a decent understanding of basic functions within laravel, now i want to be able to search record's from my database and i cant seem to figure out how to do it,
This is the Controller Function i've made for it
public function search()
{
//searching on producten in the producten database table
$producten = DB::table('producten')
->where('naam', 'LIKE', '%$query%')
->get();
}
and this is what My View looks like
{!! Form::open(['route' => 'producten.index', 'method' => 'GET', 'class' => 'search']) !!}
{!! Form::text('naam')!!}
{!! Form::close() !!}
if i go on the view in my browser, and type a Product name into the text box, the url changes to producten?naam=ProductNaam but nothing different is showen on the screen, i hope this is enough information to help me, just let me know if i need to add anything to the question.
Upvotes: 2
Views: 1839
Reputation: 13254
Your view is correct , but your controller doesn't accept anything.
You are currently searching for the products where the name has 'query' inside it.
So you have to accept the Request inside your search method.
Like so:
public function search(Request $request)
$request->naam
will give you the value you typed inside your text field. Obviously you still need to validate that value.
I would also recommend filtering on the Model, your code will be more readable.
Product::where('naam', 'LIKE', "%$request->naam%")->get();
You should read this page , this will give you a better understanding of your problem https://laravel.com/docs/5.3/requests
Upvotes: 3
Reputation: 17708
Your controller function should be as:
public function search()
{
//searching on producten in the producten database table
$query = request()->get('naam');
$producten = DB::table('producten')
->where('naam', 'LIKE', "%$query%")
->get();
// pass the `$producten` result to your search view.
}
Upvotes: 0
Reputation: 213
Just fix the quotes here: ->where('naam', 'LIKE', "%$query%")
More info can be found in the docs
Upvotes: 0