user1692333
user1692333

Reputation: 2597

How to generate url with params in Laravel?

The situation is quite tricky... For example I have URL http://example.com/users

which shows all users. I also have few filters like

http://example.com/users?sort_type=ASC&sort_value=surname

or

http://example.com/users?sort_type=DESC&sort_value=name.

There also is the search which looks like this

http://example.com/users/search/by_name/?search_value=bob

or http://example.com/users/search/by_surname/?search_value=miller.

For the search I also need to add filtering params, so the main problem is in first symbol: when there is the list of users it should be ?, when search &. So is there some url generation function for generating url from URL params?

Upvotes: 0

Views: 91

Answers (1)

Alexey Tihomirov
Alexey Tihomirov

Reputation: 49

All values not using by route mask will append as query params. (change route name from 'users' to yours)

URL::route('users', array(
  'sort_type' => 'ASC',
  'sort_value' => 'surname'
));

If you don't use routes then use something like this

$query = http_build_query(array(
      'sort_type' => 'ASC',
      'sort_value' => 'surname'
));
URL::to(action('UserController@index') . '?' . $query );

Upvotes: 1

Related Questions