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);
}
}
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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.