1

I am using laravel 5.2. I have a question. If I send a request like

http://localhost/test?sports[]=soccer&sports[]=basketball

I can get sports as an array with 2 elements.

But if I send request like:

http://localhost/test?sports[]

I get it by $sports = $request->input('sports'), I think it is an empty array, but it's actually not. I var_dump() it:

array(1) { [0]=> string(0) "" }

So someone can tell me how can I determine the request input array data is empty?


Supplement:

My routes.php here:

Route::get('/test',function(Illuminate\Http\Request $request){
    var_dump($request->has('sports'));
});

I access http://localhost/test?sports[]

It output true.

2
  • var_dump $request->has('sports') check the output should be false. Commented Dec 27, 2016 at 14:15
  • No, it output true. Commented Dec 27, 2016 at 14:38

1 Answer 1

1

I don't think there is any built-in functionality for what you need but you can create a Request macro as:

Request::macro('someCoolName', function ($key) {
    return is_array($this->input($key)) && (count($this->input($key)) == 1) && empty($this->input($key)[0]) ? [] : $this->input($key);
});

Note - Add it in boot() method of App\Providers\AppServiceProvider.

And then you can use it as:

$request->someCoolName('sports'); // returns []

It returns emprty array [] if your URL is like http://localhost/test?sports[]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.