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

前端 未结 7 1806
礼貌的吻别
礼貌的吻别 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:47

    I've actually found that using newCollection() is more future proof....

    Example:

    $collection = (new Post)->newCollection();
    

    That way, if you decide to create your own collection class for your model (like I have done several times) at a later stage, it's much easier to refactor your code, as you just override the newCollection() function in your model

提交回复
热议问题