简体   繁体   English

从集合中删除行更改集合的类型

[英]Removing rows from a collection changing types of the collection

I have a two collections.我有两个收藏。 First one is a big collection.第一个是大集合。 Another one is remove list.另一种是删除列表。 For example:例如:

$data= List::where('status', true)->get();

$list= List::where('begin_at', '<', Carbon::now())->pluck('id');

$result = $data->whereNotIn('id', $list);

This is just a simple example.这只是一个简单的例子。 I have a dynamic data.我有一个动态数据。 My question is:我的问题是:

When i return $data , it returns [{}, {}, ...] format当我返回$data ,它返回[{}, {}, ...]格式

But when i return $result , it returns {}, {}, ...但是当我返回$result ,它返回{}, {}, ...

I tried to $result->toArray() , $result->toCollapse() but none of them is worked.我试图$result->toArray()$result->toCollapse()但它们都$result->toCollapse()

Why after the get() , using where condition changing the type of the collection?为什么在get() ,使用 where 条件改变集合的类型? Why array symbols gone?为什么数组符号不见了? What is the best practices in here to solution?这里解决的最佳实践是什么? Thanks in advance.提前致谢。

Use all() on the collection to get it as an array with the Eloquent models intact.在集合上使用all()将其作为一个数组,并保留完整的 Eloquent 模型。 If you use toArray() instead, it will convert everything to an plain PHP array.如果您改用toArray() ,它会将所有内容转换为普通的 PHP 数组。

$data->whereNotIn('id', $list)->all();

See docs here在此处查看文档

Calling get() on a model will create a collection, and the collection upon creation will call all() on itself.对模型调用get()将创建一个集合,创建时的集合将调用all()自身。 So after modifying the collection you will need to call all() yourself to the the same "format" as from get() .因此,在修改集合后,您需要将all()自己调用为与get()相同的“格式”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM