How to manually create a new empty Eloquent Collection in Laravel 4

前端 未结 7 1852
礼貌的吻别
礼貌的吻别 2020-12-14 14:21

How do we create a new Eloquent Collection in Laravel 4, without using Query Builder?

There is a newCollection() method which can be overridden by that

7条回答
  •  爱一瞬间的悲伤
    2020-12-14 14:31

    In Laravel 5 and Laravel 6 you can resolve the Illuminate\Database\Eloquent\Collection class out of the service container and then add models into it.

    $eloquentCollection = resolve(Illuminate\Database\Eloquent\Collection::class);
    // or app(Illuminate\Support\Collection::class). Whatever you prefer, app() and resolve() do the same thing.
    
    $eloquentCollection->push(User::first());
    

    For more information about understanding resolving objects out of the service container in laravel take a look here: https://laravel.com/docs/5.7/container#resolving

提交回复
热议问题