JacopoStanchi
JacopoStanchi

Reputation: 2136

Pass multiple values of a HTTP request parameter

How to pass multiple values of a single HTTP request parameter, and retrieve them in the controller?

Whether it be a repeated parameter like so:

http://example.com/users?q=1&q=2

or multiple values in a row like that:

http://example.com/users?q=1,2

Thank you for your help.

Upvotes: 0

Views: 4237

Answers (2)

Dexter Bengil
Dexter Bengil

Reputation: 6615

Just like when you pass an html input with a value of array, you can pass it with []. e.g. /users?q[]=1&q[]=2

Route::get('users', function (Illuminate\Http\Request $request) {
    // when you dump the q parameter, you'll get:
    dd($request->q);
    // q = [1, 2]
});

Upvotes: 3

Chin Leung
Chin Leung

Reputation: 14921

You can pass an array to the request like this:

http://example.com/users?q[]=1&q[]=2

The [] will pass the parameter as an array. Therefore, when you retrieve the q from the request:

dd(request('q'));

It will give you the following:

array:2 [▼
  0 => "1"
  1 => "2"
]

Upvotes: 4

Related Questions