簡體   English   中英

如何在Laravel中手動新建一個空的Eloquent Collection 4

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

我們如何在 Laravel 4 中創建一個新的 Eloquent 集合,而不使用查詢生成器?

有一個newCollection()方法可以被它覆蓋,但它並沒有真正起作用,因為它只在我們查詢一組結果時使用。

我正在考慮構建一個空的 Collection,然后用 Eloquent 個對象填充它。 我不使用數組的原因是因為我喜歡 Eloquent Collections 方法,例如contains

如果還有其他選擇,我很樂意聽取他們的意見。

這不是真正的 Eloquent,要將 Eloquent 模型添加到您的集合中,您有一些選擇:

Laravel 5 中,您可以從助手中受益

$c = collect(new Post);

或者

$c = collect();
$c->add(new Post);

舊 Laravel 4 答案

$c = new \Illuminate\Database\Eloquent\Collection;

然后你可以

$c->add(new Post);

或者你可以使用 make:

$c = Collection::make(new Post);

從 Laravel 5. 我使用全局函數collect()

$collection = collect([]); // initialize an empty array [] inside to start empty collection

這種語法非常干凈,如果您不想要數字索引,也可以添加偏移量,如下所示:

$collection->offsetSet('foo', $foo_data); // similar to add function but with
$collection->offsetSet('bar', $bar_data); // an assigned index

我實際上發現使用newCollection()更具前瞻性......

例子:

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

這樣,如果您決定在稍后階段為您的模型創建自己的集合類(就像我已經做過幾次一樣),那么重構代碼會容易得多,因為您只需覆蓋模型中的newCollection()函數

只是為了添加到已接受的答案,您還可以在config/app.php創建別名

'aliases' => array(

    ...
    'Collection'      => Illuminate\Database\Eloquent\Collection::class,

然后你只需要做

$c = new Collection;

Laravel >= 5.5

這可能與原始問題無關,但由於它是 google 搜索中的第一個鏈接之一,我發現這對像我這樣正在尋找如何創建空集合的人很有幫助。

如果你想手動創建一個新的空集合,你可以像這樣使用collect輔助方法:

$new_empty_collection = collect();

你可以在Illuminate\\Support\\helpers.php找到這個助手

片段:

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);
    }
}

最好使用注入模式並且在$this->collection->make([])不是new Collection

use Illuminate\Support\Collection;
...
// Inside of a clase.
...
public function __construct(Collection $collection){
    $this->collection = $collection;
}

public function getResults(){
...
$results = $this->collection->make([]);
...
}

在 Laravel 5 和 Laravel 6 中,您可以從服務容器中解析Illuminate\\Database\\Eloquent\\Collection類,然后將模型添加到其中。

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

$eloquentCollection->push(User::first());

有關了解從 laravel 中的服務容器解析對象的更多信息,請查看此處: https ://laravel.com/docs/5.7/container#resolving

我正在使用這種方式:

$coll = new Collection();
    
$coll->name = 'name';
$coll->value = 'value';
$coll->description = 'description';

並將其用作普通收藏

dd($coll->name);

對我有用的是命名 use 命名空間並直接實例化它:

use Illuminate\Database\Eloquent\Collection as EloquentCollection;

# Usage
$this->latest_posts = new EloquentCollection();

允許我合並 eloquent 收集結果的兩個數據子集,這維護了關系 - 常規收集( collect() )失去了關系,可能還有一些元數據。

$limit = 5;
$this->latest_posts = new EloquentCollection();

$pinned_posts = PinnedPostReference::where('category', $category)->get();
if($pinned_posts->count() > 0) {
    foreach($pinned_posts as $ppost) {
        $this->latest_posts->push($ppost->post);
    }
}

# Another Eloquent result set ($regular_posts)
foreach($regular_posts as $regular_post) {
    $this->latest_posts->push($regular_post);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM