Reputation: 115
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);
}
}
Upvotes: 0
Views: 1773
Reputation: 115
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());
}
Upvotes: 2