Bla*_*car 8 laravel laravel-5.3 laravel-5.4
我收集了Laravel:
Collection {#450 ?
#items: array:2 [?
0 => Announcement {#533 ?}
1 => Announcement {#553 ?}
]
}
Run Code Online (Sandbox Code Playgroud)
这是相同的项目.如何删除其中一个?
完整代码是:
public function announcements()
{
$announcements = $this->categories_ann->map(function ($c) {
return $c->announcements->map(function ($a) {
$a->subsribed = true;
return $a;
});
});
$flattened = $announcements->groupBy("id")->flatten();
return $flattened;
}
Run Code Online (Sandbox Code Playgroud)
Ric*_*ard 23
$collection = collect([
['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
['name' => 'iPhone 5', 'brand' => 'Apple', 'type' => 'phone'],
['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],
['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],
]);
Run Code Online (Sandbox Code Playgroud)
然后假设您希望品牌独一无二,在这种情况下,您应该只获得“Apple”和“Samsung”两个品牌
$unique = $collection->unique('brand');
$unique->values()->all();
/*
[
['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
]
*/
Run Code Online (Sandbox Code Playgroud)
这取自https://laravel.com/docs/master/collections#method-unique
| 归档时间: |
|
| 查看次数: |
8565 次 |
| 最近记录: |