0

I have a DDBB that I'm allready consuming with STRAPI, and I get data via REST API.

I can make this request and the data is automatically filtered...

http://localhost:1337/filial-cruzas?_limit=-1&filial_id=543&activa=1

But I need to convert this to laravel, I allready get information with the base Route:

Route::get('filialcruzas', 'FilialCruzaController@index');

and of course I have this route also:

Route::get('filialcruzas/{id}', 'FilialCruzaController@show');

So I can get a list or a single record.

But I need to filter the list, in the backend, like Strapi do.

I just added Request param to index method, but is allways empty. What do I have to do to receive all the URL ? Then I figure out how to parse and use what I need ...

This is mi Controller

class FilialCruzaController extends Controller
{
    //
        /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //

        $filialcruzas=Cache::remember('cachefc',15/60,function()
        {
            return FilialCruza::simplePaginate(10);  // Paginamos cada 10 elementos.

        });

        return response()->json(['status'=>'ok',
            'request' => $request,
            'data'=>$filialcruzas],200);
 }
...
}

Any idea will be appreciatted!! Best Regards!

Nicolas400
  • 563
  • 1
  • 7
  • 29

1 Answers1

1

You can accept the route parameter as a parameter in the controller method

class FilialCruzaController extends Controller
{
    //
        /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $id)
    {
        //Do the filtering based on the $id

        //Get all parameters from query string as an associative array
        $queryParams = $request->query();

        $filialcruzas=Cache::remember('cachefc',15/60,function()
        {
            return FilialCruza::simplePaginate(10);  // Paginamos cada 10 elementos.

        });

        return response()->json(['status'=>'ok',
            'request' => $request,
            'data'=>$filialcruzas],200);
 }
...
}

You can get all query parameters from the query string as an associative array of the incoming request using

$request->query()

Laravel docs:https://laravel.com/docs/8.x/requests#retrieving-input-from-the-query-string

Donkarnash
  • 12,433
  • 5
  • 26
  • 37
  • Please vote to close as duplicate when applicable. – miken32 Dec 01 '20 at 19:50
  • I was vardumping $request, and the array of query() was empty, but when I var_dump($request->query) I see what i want ! Thanks! – Nicolas400 Dec 01 '20 at 19:53
  • @miken32 Before I posted my answer didn't see your comment. And after seeing the linked post, I feel that it's quite similar but the OP also had a question why the route parameter is always empty. – Donkarnash Dec 01 '20 at 19:54
  • The comment about route parameters was saying that it worked. OP does not want an `$id` parameter on `index()` method. – miken32 Dec 01 '20 at 19:58
  • @miken32 "I just added Request param to index method, but is allways empty." made me believe that OP is trying to get the route param in the controller method. Anyways as I said I didn't see your comment before posting my answer – Donkarnash Dec 01 '20 at 20:01