Skip to content
Advertisement

Laravel: how to pass GET array to controller?

How to pass array of GET parameters to the controller?

This is my route file routes/web.php:

<?php
use IlluminateSupportFacadesRoute;
use GuzzleHttpClient;

Route::get('/ids', 'Parser@getIds');

And my controller ‘app/Http/Controllers/Parser.php’:

<?php

namespace AppHttpControllers;

use AppHttpControllersController;
use IlluminateHttpRequest;

class Parser extends Controller
{
  public function getIds(Request $request) {
    return response()->json($request); // ???
  } 
}

So, I expect to get an array of parameters like this:

$ids = [1,2,15,25];

But if I pass GET array to my route path: http://example.com/ids?ids[]=1&ids[]=2&ids[]=15&ids[]=25

I get an empty request object anyway:

{"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}

Advertisement

Answer

There is few ways to get data from GET. Try something like that:

$ids = request('ids', []); // global helper, empty array as default

or:

$ids = $request->input('ids', []); // via injected instance of Request

More info you can get in documentation

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement