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

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

    Laravel >= 5.5

    This may not be related to the original question, but since it's one of the first link in google search, i find this helpful for those like me, who are looking for how to create empty collection.

    If you want to manually create a new empty collection, you can use the collect helper method like this:

    $new_empty_collection = collect();
    

    You can find this helper in Illuminate\Support\helpers.php

    snippet:

    if (! function_exists('collect')) {
        /**
         * Create a collection from the given value.
         *
         * @param  mixed  $value
         * @return \Illuminate\Support\Collection
         */
        function collect($value = null)
        {
            return new Collection($value);
        }
    }
    

提交回复
热议问题