@prido I don’t really know what you’re illustration, but you shouldn’t be fetching all records from your database, just to then iterate over them and try and JSON-encode them. If you have a lot of records, then you’re going to hit out-of-memory errors.
Instead, send paginated data sets to end users. And if you need to change the “shape” of records returned when they’re serialised as JSON, use Eloquent API resource classes to transform your models.