Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmaneesa johnson
2,618 PointsNewbie: Trying to create a search function on laravel 5
I'm trying to make a search function with laravel 5 and it's not passing the results variable to the view, has I written it incorrectly? It says $results variable undefined without the isset function and it always says products not set. When I var dump the information it says: string '$results' (length=8).
Search controller:
public function index()
{
$searchterm = Input::get('searchinput');
if ($searchterm){
$products = DB::table('products');
$results = $products->where('name', 'LIKE', '%'. $searchterm .'%')
->orWhere('description', 'LIKE', '%'. $searchterm .'%')
->orWhere('brand', 'LIKE', '%'. $searchterm .'%')
->get();
return view('search')->with('products', $results);
}
}
The view: <?php if(isset($results)){ ?> @foreach($results as $result) {{{ $result->name }}} @endforeach <?php } else { echo "products not set"; } ?>
2 Answers
Petros Sordinas
16,181 PointsHi Maneesa,
Here is your problem: return view('search')->with('products', $results);
You are loading the search view passing the $results as products. Your view does a foreach loop on $results, not $products. Either change $results to $products in your view or load your view like this:
return view('search')->with('results', $results);
maneesa johnson
2,618 PointsHey thanks for getting back to me. I made the changes but now I get an error saying call to non object. I'm trying to find out why. Do you have any ideas?