I would like to create a filter in a search function in Laravel 5.5, but I'm quite new and pretty much stuck at this point.
I used this tutorial: http://ift.tt/2tV463b
The search function itself works, but I am not sure how to add the filter itself. The filter should be basically be a dropdown on a blade.php which updates a variable which should change the controller I want to look in and the search criteria.
The code from my routing:
use App\Item;
use Illuminate\Support\Facades\Input;
Route::any('/', function () {
$q = Input::get ('q');
$item = Item::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'description', 'LIKE', '%' . $q . '%' )->get();
if (count($item) > 0)
return view ('pages.index')->withDetails($item)->withQuery($q)->with('items', $item);
else
return view ('pages.index')->withMessage ('Nothing found.')->with('items', $item);
});
Where it now says 'description' after in the query after $item, I would like 'description' to be variable and pass along the value from a dropdown that comes from a view so a user can refine their search. In theory this sounds simple as it's simple to do in PHP, but because of the MVC setup it's a bit confusing to me and it almost seems like bad practice to do this in the route?
I am wondering if this solution would be better as it seems to make more sense to do it in a controller than the route: Creating search functionality with Laravel 4
The code from that stackoverflow page:
public function search() {
$q = Input::get('myInputField');
$searchTerms = explode(' ', $q);
$query = DB::table('products');
foreach($searchTerms as $term)
{
$query->where('name', 'LIKE', '%'. $term .'%');
}
$results = $query->get();
}
But in that case, how would I be able to add a variable into it that can be linked to a dropdown in a view? I've been looking for a way to do that, it's usually that you pass along a variable from a controller to a view and not the other way around. I could create an array in the search method and then pass that along to the view the below line, but is that the right way to do this?:
return view('pages.index')->with('items', $items);
Perhaps I'm looking at this completely the wrong way. Any help to guide me in the right direction would be appreciated.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ClR03u
via IFTTT
I followed this tutorial to create a Laravel search engine. Can I apply this filter script in my Laravel based search engine? Would it work with the script?
RépondreSupprimer