0

I have inputs array and i need to make a foreach but laravel $request->all() only return last one:

url:

http://localhost:8000/api/ofertas?filter_pais=1&filter_pais=2&filter_pais=3

controller:

public function filtroOfertas(Request $request){
        return $request->all();
}

result:

{"filter_pais":"3"}

result should return 1, 2 and 3 and i need to make a foreach in filter_pais. Any solution? Thanks

2 Answers 2

1

Use [] at the key of query string.

http://localhost:8000/api/ofertas?filter_pais[]=1&filter_pais[]=2&filter_pais[]=3

It will be parsed as array.

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

Comments

0

Repeated parameters make no sense and should be avoided without exception.

But looking at other solutions, there are several:

routes.php

Route::get('/api/ofertas/{r}', 'Controller@index');

Controller:

    public function index($r)
    {
        $query  = explode('&', $r);
        $params = array();

        foreach($query as $param)
        {
            list($name, $value) = explode('=', $param);
            $params[urldecode($name)][] = urldecode($value);
        }

        // $params contains all parameters

}

Given that the URL has no question marks:

http://localhost:8000/api/ofertas/filter_pais=1&filter_pais=2&filter_pais=3

Comments

Your Answer

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