0

I would like to add query params to my rest api. I created that in Resource Collections and now I cant find a way to add this functionality. Every tutorial is for other ways of creating api. I would like to add to endpoint /api/v1/product filtering by product code something like this: /api/v1/product?product_code=0208588343711. This is my code

Product Controller:

public function index(): ProductCollection
{
    return new ProductCollection(Product::paginate());
}

ProductCollection

class ProductCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}
1
  • You can filter your products before passing them to the ProductCollection Commented Nov 11, 2019 at 10:55

1 Answer 1

2

If someone need a answer so I did this in this way:

public function index(): ProductCollection
    {
        if (request()->input('product_code')){
            return new ProductCollection(Product::where('product_code', 'LIKE', request()->input('product_code'))->get());
        }
        return new ProductCollection(Product::paginate());
    }
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.