php Laravel eloquent 集合上的 Array_unique

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18626599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:55:47  来源:igfitidea点击:

Array_unique on a laravel eloquent collection

phplaravellaravel-4eloquentarray-unique

提问by AndrewMcLagan

Not sure if this is possible but im trying to run array_unique over a collection of items i have, to remove duplicates. Although i cannot get it working.

不确定这是否可行,但我尝试在我拥有的一组项目上运行 array_unique,以删除重复项。虽然我无法让它工作。

my controller logic:

我的控制器逻辑:

    // init models
    $jobs = Job::search();
    $countries = $jobs->get()->map(function( $job ) {

        return $job->country;
    });
    $countries = array_unique( $countries->toArray() );

although this gets a "Array to string conversion" error

虽然这会得到一个“数组到字符串转换”的错误

采纳答案by Rubens Mariuzzo

You can have unique values in your DB results using distinctor group byin your select clause. But, if you reallyneed to have unique values over an array of object you can do the following:

您可以使用distinctgroup by在您的选择子句中在您的数据库结果中使用唯一值。但是,如果您确实需要在对象数组上具有唯一值,则可以执行以下操作:

$uniques = array();
foreach ($countries as $c) {
    $uniques[$c->code] = $c; // Get unique country by code.
}

dd($uniques);

回答by Jorr.it

You could try the Unique method of the Collection class:

您可以尝试 Collection 类的 Unique 方法:

$countries = $countries->unique();

The Collection class has several wonderful methods. You could read about this in the Laravel API documentation.

Collection 类有几个很棒的方法。您可以在Laravel API 文档 中阅读相关内容。

I agree that sometimes it is more efficient to "query" on an existing Collection in memory (in stead of doing another query on the database using the Querybuilder class), like for example you first want to count and then filter. In .NET LINQ you can query almost equally on an IEnumerable (in-memory collection) as on a database, something I really enjoy.

我同意有时在内存中的现有集合上“查询”更有效(而不是使用 Querybuilder 类对数据库进行另一个查询),例如您首先想要计数然后过滤。在 .NET LINQ 中,您可以在 IEnumerable(内存中集合)上查询几乎与在数据库上相同的查询,这是我非常喜欢的。

回答by orustammanapov

I had similar issue and although time have passed since it may be useful to someone today.

我有类似的问题,虽然时间已经过去了,因为它今天可能对某人有用。

The Problem I had was that when I called uniquemethod on collection of items it didn't worked, that is probably the reason the first answer got accepted. So if you have models and you want to remove duplicates based on a specific field you can pass parameter to your uniquemethod, in this case it would be:

我遇到的问题是,当我unique在项目集合上调用方法时它不起作用,这可能是第一个答案被接受的原因。因此,如果您有模型并且想要根据特定字段删除重复项,您可以将参数传递给您的unique方法,在这种情况下,它将是:

$countries->unique('code');

that way you'll only have countries with unique codes. You may notice that only the first value stays, so if you develop a shopping cart application and want for some reason merge carts and only want to have recent items you can just reverse the collection and call uniqueand reverse it back:

这样,您将只拥有具有唯一代码的国家/地区。您可能会注意到只有第一个值会保留,因此如果您开发一个购物车应用程序并且出于某种原因想要合并购物车并且只想拥有最近的项目,您可以只反转集合并调用unique并将其反转回来:

$countries->reverse()->unique('code')->reverse(); // it doesn't really make sense in this example though

it is probably not the best option and it is better to do filtering on the database side but it is nice to have options.

这可能不是最好的选择,最好在数据库端进行过滤,但有选项是很好的。

回答by kaning

You could try the filter method on eloquent collections if that's exactly what you want to do

如果这正是您想要做的,您可以在 eloquent 集合上尝试 filter 方法

http://laravel.com/docs/eloquent#collections

http://laravel.com/docs/eloquent#collections