laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

[Proposal] Allow Lazy Collection objects as API Resource parameters. #1847

Open jaimeescano opened 5 years ago

jaimeescano commented 5 years ago

Usually, if you would like to pull all the users from a API, you would create something like this in your controller:

use App\Http\Resources\UserResource;
use App\User;

public function index(){
    return UserResource::collection(User::all());
}

This would probably have an impact within the server memory consumption. Thanksfully, now with Laravel 6.0, we have the option to use the cursor() feature in our models.

So, instead ... we could execute the following:

use App\Http\Resources\UserResource;
use App\User;

public function index(){
    return UserResource::collection(User::cursor());
}

Currently this code will trigger and exception Call to undefined method Generator::first()

What do you think?

kasus commented 3 years ago

apparently it works fine already with Lazy Collections