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!

0

1 Answer 1

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

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

5 Comments

Please vote to close as duplicate when applicable.
I was vardumping $request, and the array of query() was empty, but when I var_dump($request->query) I see what i want ! Thanks!
@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.
The comment about route parameters was saying that it worked. OP does not want an $id parameter on index() method.
@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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.