I have a search form in the nav bar on my Laravel 4 app. When someone types into search and submits I want the queries to only be performed based on what section of the site they are on. Example, if they are on the blog page, I want it to return only blog posts, and if they are on the users page, I want it to only bring up a list of users.
Unless I'm missing something (certainly possibly as I'm new to Laravel!) I can do this using getCurrentRoute()->getPath(). But I have to do this from the form, because when I try to do it in a Controller it does not have the old route any more. So, I created a hidden field on my form like so:
{{ Form::hidden('route', '{{{Route::getCurrentRoute()->getPath()}}}') }};
However, the "getCurrentRoute()->getPath()" is never evaluated properly. The form field literally prints it out:
<input name="route" type="hidden" value="<?php echo e(Route::getCurrentRoute()->getPath()); ?>">
Does anyone know a way to get this to work, or is there a better way to do this that I'm totally missing? Again, I can't insert this into a controller like normal because at that point the route has been sent to
// Search
Route::post('search', array('as' => 'search', 'uses' => 'SearchController@postSearch'), function(){
});
At which point it always thinks the route name is "search".
Try this:
{{ Form::hidden('route', Route::getCurrentRoute()->getPath()) }}
Because it is already inside a PHP block..
If you are looking for the named route then use:
Route::currentRouteName()
Would return search
for the example you gave.
Relevant documentation http://laravel.com/docs/routing#named-routes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With