Axeia
450
6
Laravel

How to use search parameters in the old fashioned format (search?name=value)

Posted 2 years ago by Axeia

Hello, How do I get laravel to use an url in this format? search?name=akb&year=2011-2016&size=8-100? What I did so far is create this route:

Route::get('/search/{searchQuery}', 'HomeController@search');

And my HomeController has the following method:

    public function search(Requests\SearchRequest $searchQuery)
    {
        return 'Search route works!';
    }

The SearchRequest class is nothing special atm, just extending the base request and overwriting the rules method with

    public function rules()
    {
        return [
            'name'   => 'max:155',
            'year'   => ['regex:/^20[0-9]{2}:20[0-9]{2}$/'], //2000:2099
            'sizes'  => ['regex:/^[0-9]{1,3}:[0-9]{1,3}$/'], // 0-999:0-999
            'colors' => '',
        ];
    }

If I visit MySite/search?name=akb&year=2011-2016&size=8-100 I get a error NotFoundHttpException in RouteCollection.php line 161:

Preferably it would only accept urls with the questionmark?key=value markup as well instead of /key/value. And to make things possibly more complicated, the 'colors' source you can see in the validation rules is a <select multiple> so if no values are selected it won't be part of the url.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.