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

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

    0 讨论(0)
提交回复
热议问题