Mảng Laravel cho Bộ sưu tập Eloquent

Bộ sưu tập của Laravel thành mảng [phương thức toArray] sẽ cho phép chúng ta chuyển đổi thành một mảng PHP đơn giản vì một số lý do chúng ta cần sử dụng điều này trong bất kỳ tình huống nào để chúng ta có thể truy cập bản ghi thông qua một mảng

Trong blog này, chúng ta sẽ thảo luận về cách chuyển đổi một mảng PHP thành một bộ sưu tập laravel. cách chuyển đổi một mảng nhiều chiều thành một đối tượng bộ sưu tập. Here we check the proper way to convert array to collection


Laravel change simple array to collection

We can change a simple array to collection object by collect[] method. pass an array in the collect method and perform all collection operations on this array

public function index[] {
    $arr = [1,3,5,7,9];
    $collection = collect[$arr];
    dd[$collection];
}

Laravel convert multi dimensional array to collection

You can also convert multi dimension array to collection by collect method and use object method with ever array

public function index[] {
  
    $collection = collect[[
        [object] ['id'=>1, 'name'=>'Anmol', 'country'=>'India'],
        [object] ['id'=>2, 'name'=>'Krishan', 'country'=>'India'],
        [object] ['id'=>3, 'name'=>'Alok', 'country'=>'US']
    ]];

	dd[$collection];
}

Read Also . Laravel nested relationship with eager loading

Laravel push an array to collection

Use laravel push[] method use to add an array to collection object

public function index[] {
  
    $collection = new Collection[];

    $collection->push[[object]['id'=>1, 'name'=>'Anmol', 'country'=>'India']];
      
    dd[$collection];
}

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs

Today, We want to share with you Laravel Convert Array To Collection Example. Trong bài đăng này, chúng tôi sẽ chỉ cho bạn Chuyển đổi đệ quy của mảng thành tập hợp, hãy nghe để chuyển đổi mảng thành Laravel Eloquent Collection, chúng tôi sẽ cung cấp cho bạn bản demo và ví dụ để thực hiện. In this post, we will learn about laravel convert multidimensional array to collection with an example

Laravel Chuyển đổi mảng thành tập hợp ví dụ

nội dung

Cũng đọc phần này 👉   Laravel Eloquent với Bộ sưu tập chứa

Có những điều sau đây Đơn giản về Laravel Chuyển đổi mảng thành tập hợp Ví dụ Thông tin đầy đủ với ví dụ và mã nguồn

Vì tôi sẽ giới thiệu Bài đăng này với ví dụ Hoạt động trực tiếp để phát triển Chuyển đổi mảng thành Bộ sưu tập trong Laravel, do đó, bộ sưu tập laravel thành mảng đối tượng cho ví dụ này như sau bên dưới

Lớp

$collection = collect[[1, 2, 3]];

99 cung cấp một trình bao bọc trôi chảy, thuận tiện để làm việc với các mảng dữ liệu. Ví dụ: kiểm tra đoạn mã sau. Chúng ta sẽ sử dụng trình trợ giúp

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

00 để tạo một thể hiện bộ sưu tập mới từ mảng, chạy hàm

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

01 trên từng phần tử, sau đó xóa tất cả các phần tử trống

$collection = collect[['taylor', 'abigail', null]]->map[function [$name] {

}]->reject[function [$name] {

Như bạn có thể thấy, lớp

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 cho phép bạn xâu chuỗi các phương thức của nó để thực hiện ánh xạ trôi chảy và rút gọn mảng bên dưới. Nói chung, các bộ sưu tập là bất biến, nghĩa là mọi phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 đều trả về một thể hiện hoàn toàn mới của

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02

Creating Collections

As mentioned above, the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

00 helper returns a new

$collection = collect[[1, 2, 3]];

99 instance for the given array. So, creating a collection is as simple as

$collection = collect[[1, 2, 3]];

Note
The results of Eloquent queries are always returned as

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 instances

Extending Collections

Collections are "macroable", which allows you to add additional methods to the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 class at run time. The

$collection = collect[[1, 2, 3]];

99 class'

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

10 method accepts a closure that will be executed when your macro is called. The macro closure may access the collection's other methods via

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

11, just as if it were a real method of the collection class. For example, the following code adds a

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

12 method to the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 class

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

Typically, you should declare collection macros in the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

14 method of a service provider

Macro Arguments

If necessary, you may define macros that accept additional arguments

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

Available Methods

For the majority of the remaining collection documentation, we'll discuss each method available on the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 class. Remember, all of these methods may be chained to fluently manipulate the underlying array. Furthermore, almost every method returns a new

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 instance, allowing you to preserve the original copy of the collection when necessary

Method Listing

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

17

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

18 method returns the underlying array represented by the collection

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

19

Alias for the method

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

21

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

20 method returns the average value of a given key

$average = collect[[1, 1, 2, 4]]->avg[];

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

23

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

24 method breaks the collection into multiple, smaller collections of a given size

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

This method is especially useful in views when working with a grid system such as Bootstrap. For example, imagine you have a collection of Eloquent models you want to display in a grid

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

25

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

26 method breaks the collection into multiple, smaller collections based on the evaluation of the given callback. The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

27 variable passed to the closure may be used to inspect the previous element

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

28

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

29 method collapses a collection of arrays into a single, flat collection

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

30

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

00 method returns a new

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 instance with the items currently in the collection

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

00 method is primarily useful for converting into standard

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 instances

$collection = collect[[1, 2, 3]];

0

Note
The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

00 method is especially useful when you have an instance of

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

36 and need a non-lazy collection instance. Since

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

30 is part of the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

36 contract, you can safely use it to get a

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 instance

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

40

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

41 method combines the values of the collection, as keys, with the values of another array or collection

$collection = collect[[1, 2, 3]];

1

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

42

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

43 method appends the given

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

44 or collection's values onto the end of another collection

$collection = collect[[1, 2, 3]];

2

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

43 method numerically reindexes keys for items concatenated onto the original collection. To maintain keys in associative collections, see the method

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

46

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

47 method determines whether the collection contains a given item. You may pass a closure to the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

47 method to determine if an element exists in the collection matching a given truth test

$collection = collect[[1, 2, 3]];

3

Alternatively, you may pass a string to the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

47 method to determine whether the collection contains a given item value

$collection = collect[[1, 2, 3]];

4

You may also pass a key / value pair to the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

47 method, which will determine if the given pair exists in the collection

$collection = collect[[1, 2, 3]];

5

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

47 method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the method to filter using "strict" comparisons

For the inverse of

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

47, see the method

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

54

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

55 method determines whether the collection contains a single item

$collection = collect[[1, 2, 3]];

6

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

56

This method has the same signature as the method; however, all values are compared using "strict" comparisons

Note
This method's behavior is modified when using

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

58

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

59 method returns the total number of items in the collection

$collection = collect[[1, 2, 3]];

7

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

60

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

61 method counts the occurrences of values in the collection. By default, the method counts the occurrences of every element, allowing you to count certain "types" of elements in the collection

$collection = collect[[1, 2, 3]];

8

You pass a closure to the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

61 method to count all items by a custom value

$collection = collect[[1, 2, 3]];

9

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

63

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

64 method cross joins the collection's values among the given arrays or collections, returning a Cartesian product with all possible permutations

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

0

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

65

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

66 method dumps the collection's items and ends execution of the script

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

1

If you do not want to stop executing the script, use the method instead

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

68

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

69 so sánh bộ sưu tập với một bộ sưu tập khác hoặc một PHP

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

44 đơn giản dựa trên các giá trị của nó. This method will return the values in the original collection that are not present in the given collection

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

2

Note
This method's behavior is modified when using

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

71

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

72 so sánh bộ sưu tập với bộ sưu tập khác hoặc một PHP

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

44 đơn giản dựa trên các khóa và giá trị của nó. This method will return the key / value pairs in the original collection that are not present in the given collection

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

3

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

74

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

75 method compares the collection against another collection or a plain PHP

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

44 based on its keys. This method will return the key / value pairs in the original collection that are not present in the given collection

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

4

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

77

Phương pháp

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

78 xác định xem bộ sưu tập không chứa một mục nhất định hay không. Bạn có thể chuyển một bao đóng cho phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

78 để xác định xem một phần tử không tồn tại trong tập hợp có khớp với một bài kiểm tra sự thật đã cho hay không

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

5

Ngoài ra, bạn có thể chuyển một chuỗi tới phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

78 để xác định xem bộ sưu tập có chứa giá trị mục đã cho hay không

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

6

Bạn cũng có thể chuyển một cặp khóa/giá trị cho phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

78, phương thức này sẽ xác định xem cặp đã cho không tồn tại trong bộ sưu tập

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

7

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

78 sử dụng so sánh "lỏng lẻo" khi kiểm tra giá trị mục, nghĩa là một chuỗi có giá trị nguyên sẽ được coi là bằng với một số nguyên có cùng giá trị

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

83

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

67 kết xuất các mục của bộ sưu tập

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

1

Nếu bạn muốn dừng thực thi tập lệnh sau khi kết xuất bộ sưu tập, hãy sử dụng phương thức này để thay thế

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

86

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

87 truy xuất và trả về các giá trị trùng lặp từ bộ sưu tập

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

9

If the collection contains arrays or objects, you can pass the key of the attributes that you wish to check for duplicate values

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

0

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

88

This method has the same signature as the method; however, all values are compared using "strict" comparisons

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

90

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

91 method iterates over the items in the collection and passes each item to a closure

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

1

If you would like to stop iterating through the items, you may return

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

92 from your closure

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

1

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

93

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

94 method iterates over the collection's items, passing each nested item value into the given callback

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

3

You may stop iterating through the items by returning

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

92 from the callback

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

4

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

96

The

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

97 method may be used to verify that all elements of a collection pass a given truth test

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

5

If the collection is empty, the

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

97 method will return true

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

6

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

99

The

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

00 method returns all items in the collection except for those with the specified keys

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

7

For the inverse of

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

00, see the method

Note
This method's behavior is modified when using

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

02

The

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

03 method filters the collection using the given callback, keeping only those items that pass a given truth test

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

8

If no callback is supplied, all entries of the collection that are equivalent to

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

92 will be removed

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

9

For the inverse of

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

03, see the method

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

06

The

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

07 method returns the first element in the collection that passes a given truth test

$average = collect[[1, 1, 2, 4]]->avg[];

0

You may also call the

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

07 method with no arguments to get the first element in the collection. If the collection is empty,

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

09 is returned

$average = collect[[1, 1, 2, 4]]->avg[];

1

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

10

The

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

11 method is identical to the

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

07 method; however, if no result is found, an

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

13 exception will be thrown

$average = collect[[1, 1, 2, 4]]->avg[];

2

Bạn cũng có thể gọi phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

11 không có đối số để lấy phần tử đầu tiên trong tập hợp. Nếu bộ sưu tập trống, một ngoại lệ

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

13 sẽ được đưa ra

$average = collect[[1, 1, 2, 4]]->avg[];

3

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

16

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

17 trả về phần tử đầu tiên trong bộ sưu tập với cặp khóa/giá trị đã cho

$average = collect[[1, 1, 2, 4]]->avg[];

4

Bạn cũng có thể gọi phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

17 với toán tử so sánh

$average = collect[[1, 1, 2, 4]]->avg[];

5

Giống như phương thức, bạn có thể truyền một đối số cho phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

17. Trong trường hợp này, phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

17 sẽ trả về mục đầu tiên có giá trị của khóa mục đã cho là "trung thực"

$average = collect[[1, 1, 2, 4]]->avg[];

6

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

21

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

22 lặp qua bộ sưu tập và chuyển từng giá trị cho bao đóng đã cho. Việc đóng cửa có thể tự do sửa đổi vật phẩm và trả lại vật phẩm đó, do đó tạo thành một bộ sưu tập mới gồm các vật phẩm đã sửa đổi. Sau đó, mảng được san bằng một cấp

$average = collect[[1, 1, 2, 4]]->avg[];

7

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

23

Phương pháp

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

24 làm phẳng một tập hợp nhiều chiều thành một chiều duy nhất

$average = collect[[1, 1, 2, 4]]->avg[];

8

Nếu cần, bạn có thể chuyển đối số "độ sâu" cho phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

24

$average = collect[[1, 1, 2, 4]]->avg[];

9

Trong ví dụ này, gọi

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

24 mà không cung cấp độ sâu cũng sẽ làm phẳng các mảng lồng nhau, dẫn đến ____22_______27. Cung cấp độ sâu cho phép bạn chỉ định số lượng các mảng lồng nhau sẽ được làm phẳng

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

28

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

29 hoán đổi các khóa của bộ sưu tập với các giá trị tương ứng của chúng

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

0

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

30

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

31 xóa một mục khỏi bộ sưu tập bằng khóa của nó

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

1

Cảnh báo
Không giống như hầu hết các phương pháp thu thập khác,

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

31 không trả về một bộ sưu tập mới đã sửa đổi;

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

33

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

34 trả về một bộ sưu tập mới chứa các mục sẽ có trên một số trang nhất định. Phương thức chấp nhận số trang làm đối số đầu tiên và số lượng mục hiển thị trên mỗi trang làm đối số thứ hai

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

2

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

35

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

36 trả về mục tại một khóa đã cho. Nếu khóa không tồn tại,

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

09 được trả về

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

3

Bạn có thể tùy chọn chuyển một giá trị mặc định làm đối số thứ hai

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

4

Bạn thậm chí có thể chuyển một cuộc gọi lại làm giá trị mặc định của phương thức. Kết quả của cuộc gọi lại sẽ được trả về nếu khóa được chỉ định không tồn tại

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

5

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

38

Phương pháp

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

39 nhóm các mục của bộ sưu tập theo một khóa nhất định

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

6

Thay vì chuyển một chuỗi

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

40, bạn có thể chuyển một cuộc gọi lại. Cuộc gọi lại sẽ trả về giá trị bạn muốn khóa nhóm theo

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

7

Nhiều tiêu chí nhóm có thể được thông qua dưới dạng một mảng. Mỗi phần tử của mảng sẽ được áp vào mức tương ứng trong mảng nhiều chiều

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

8

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

41

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

42 xác định xem một khóa đã cho có tồn tại trong bộ sưu tập hay không

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

9

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

43

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

44 xác định xem có bất kỳ khóa nào trong số các khóa đã cho tồn tại trong bộ sưu tập hay không

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

0

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

45

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

46 kết hợp các mục trong một bộ sưu tập. Đối số của nó phụ thuộc vào loại mục trong bộ sưu tập. Nếu bộ sưu tập chứa các mảng hoặc đối tượng, bạn nên chuyển khóa của các thuộc tính mà bạn muốn tham gia và chuỗi "keo" mà bạn muốn đặt giữa các giá trị

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

1

Nếu bộ sưu tập chứa các chuỗi đơn giản hoặc giá trị số, bạn nên chuyển "keo" làm đối số duy nhất cho phương thức

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

2

Bạn có thể chuyển một bao đóng cho phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

46 nếu bạn muốn định dạng các giá trị được ẩn

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

3

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

48

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

49 loại bỏ bất kỳ giá trị nào khỏi bộ sưu tập ban đầu không có trong

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

44 hoặc bộ sưu tập đã cho. Bộ sưu tập kết quả sẽ bảo toàn các khóa của bộ sưu tập gốc

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

4

Note
This method's behavior is modified when using

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

51

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

52 loại bỏ bất kỳ khóa nào và các giá trị tương ứng của chúng khỏi bộ sưu tập ban đầu không có trong

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

44 hoặc bộ sưu tập đã cho

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

5

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

54

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

55 trả về

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56 nếu bộ sưu tập trống;

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

58

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

59 trả về

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56 nếu bộ sưu tập không trống;

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

6

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

62

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

63 kết hợp các giá trị của bộ sưu tập với một chuỗi. Sử dụng đối số thứ hai của phương thức này, bạn cũng có thể chỉ định cách thêm phần tử cuối cùng vào chuỗi

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

7

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

64

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

65 khóa bộ sưu tập theo khóa đã cho. Nếu nhiều mục có cùng khóa, chỉ mục cuối cùng sẽ xuất hiện trong bộ sưu tập mới

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

8

Bạn cũng có thể chuyển một cuộc gọi lại cho phương thức. Cuộc gọi lại sẽ trả về giá trị để khóa bộ sưu tập theo

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

9

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

66

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

67 trả về tất cả các khóa của bộ sưu tập

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

0

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

68

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

69 trả về phần tử cuối cùng trong bộ sưu tập đã vượt qua bài kiểm tra độ đúng nhất định

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

1

Bạn cũng có thể gọi phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

69 không có đối số để lấy phần tử cuối cùng trong tập hợp. Nếu bộ sưu tập trống,

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

09 được trả về

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

2

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

72

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

73 trả về một thể hiện mới từ mảng các mục bên dưới

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

3

Điều này đặc biệt hữu ích khi bạn cần thực hiện các phép biến đổi trên một

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 khổng lồ chứa nhiều mục

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

4

Bằng cách chuyển đổi bộ sưu tập thành

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

74, chúng tôi tránh phải phân bổ nhiều bộ nhớ bổ sung. Mặc dù bộ sưu tập ban đầu vẫn giữ các giá trị của nó trong bộ nhớ, các bộ lọc tiếp theo sẽ không. Do đó, hầu như không có bộ nhớ bổ sung nào được phân bổ khi lọc kết quả của bộ sưu tập

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

77

Phương thức tĩnh

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

10 cho phép bạn thêm các phương thức vào lớp

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

02 trong thời gian chạy. Tham khảo tài liệu trên để biết thêm thông tin

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

80

Phương thức tĩnh

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

81 tạo một thể hiện bộ sưu tập mới. xem phần

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

82

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

83 lặp qua bộ sưu tập và chuyển từng giá trị cho cuộc gọi lại đã cho. Cuộc gọi lại có thể tự do sửa đổi mục và trả lại mục đó, do đó tạo thành một bộ sưu tập mới gồm các mục đã sửa đổi

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

5

Cảnh báo
Giống như hầu hết các phương thức thu thập khác,

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

83 trả về một thể hiện bộ sưu tập mới; . Nếu bạn muốn chuyển đổi bộ sưu tập ban đầu, hãy sử dụng phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

86

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

86 lặp lại bộ sưu tập, tạo một thể hiện mới của lớp đã cho bằng cách chuyển giá trị vào hàm tạo

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

6

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

88

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

89 lặp lại các mục của bộ sưu tập, chuyển từng giá trị mục lồng nhau vào bao đóng đã cho. Việc đóng cửa có thể tự do sửa đổi vật phẩm và trả lại vật phẩm đó, do đó tạo thành một bộ sưu tập mới gồm các vật phẩm đã sửa đổi

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

7

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

90

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

91 nhóm các mục của bộ sưu tập theo bao đóng đã cho. Việc đóng sẽ trả về một mảng kết hợp chứa một cặp khóa/giá trị duy nhất, do đó tạo thành một tập hợp các giá trị được nhóm mới

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

8

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

92

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

93 lặp qua bộ sưu tập và chuyển từng giá trị cho cuộc gọi lại đã cho. Cuộc gọi lại sẽ trả về một mảng kết hợp chứa một cặp khóa/giá trị

$collection = collect[str_split['AABBCCCD']];

$chunks = $collection->chunkWhile[function [$value, $key, $chunk] {

return $value === $chunk->last[];

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

9

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

94

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

95 trả về giá trị lớn nhất của một khóa đã cho

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

0

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

96

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

97 trả về giá trị trung bình của một khóa đã cho

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

1

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

98

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

99 hợp nhất mảng hoặc bộ sưu tập đã cho với bộ sưu tập ban đầu. Nếu khóa chuỗi trong các mục nhất định khớp với khóa chuỗi trong bộ sưu tập ban đầu, thì giá trị của mục đã cho sẽ ghi đè lên giá trị trong bộ sưu tập ban đầu

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

2

Nếu các khóa của mục nhất định là số, các giá trị sẽ được thêm vào cuối bộ sưu tập

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

3

$average = collect[[1, 1, 2, 4]]->avg[];

00

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

01 kết hợp đệ quy mảng hoặc bộ sưu tập đã cho với bộ sưu tập ban đầu. Nếu một khóa chuỗi trong các mục đã cho khớp với một khóa chuỗi trong bộ sưu tập ban đầu, thì các giá trị cho các khóa này sẽ được hợp nhất với nhau thành một mảng và điều này được thực hiện theo cách đệ quy

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

4

$average = collect[[1, 1, 2, 4]]->avg[];

02

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

03 trả về giá trị nhỏ nhất của một khóa đã cho

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

5

$average = collect[[1, 1, 2, 4]]->avg[];

04

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

05 trả về giá trị chế độ của một khóa đã cho

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

6

$average = collect[[1, 1, 2, 4]]->avg[];

06

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

07 tạo một bộ sưu tập mới bao gồm mọi phần tử thứ n

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

7

Bạn có thể tùy ý chuyển phần bù bắt đầu làm đối số thứ hai

$average = collect[[1, 1, 2, 4]]->avg[];

08

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

09 trả về các mục trong bộ sưu tập với các khóa được chỉ định

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

8

Đối với nghịch đảo của

$average = collect[[1, 1, 2, 4]]->avg[];

09, hãy xem phương pháp

Note
This method's behavior is modified when using

$average = collect[[1, 1, 2, 4]]->avg[];

11

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

12 sẽ điền vào mảng với giá trị đã cho cho đến khi mảng đạt đến kích thước đã chỉ định. Phương thức này hoạt động giống như hàm Array_pad PHP

Để đệm sang trái, bạn nên chỉ định kích thước âm. Sẽ không có phần đệm nào diễn ra nếu giá trị tuyệt đối của kích thước đã cho nhỏ hơn hoặc bằng độ dài của mảng

$collapsed = $collection->collapse[];

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

9

$average = collect[[1, 1, 2, 4]]->avg[];

13

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

14 có thể được kết hợp với việc phá hủy mảng PHP để tách các phần tử vượt qua bài kiểm tra sự thật nhất định khỏi những phần tử không

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

0

$average = collect[[1, 1, 2, 4]]->avg[];

15

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

16 chuyển bộ sưu tập tới bao đóng đã cho và trả về kết quả của bao đóng đã thực hiện

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

1

$average = collect[[1, 1, 2, 4]]->avg[];

17

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

18 tạo một thể hiện mới của lớp đã cho và chuyển bộ sưu tập vào hàm tạo

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

2

$average = collect[[1, 1, 2, 4]]->avg[];

19

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

20 chuyển bộ sưu tập tới mảng các bao đóng đã cho và trả về kết quả của các bao đóng đã thực hiện

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

3

$average = collect[[1, 1, 2, 4]]->avg[];

21

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

22 truy xuất tất cả các giá trị cho một khóa đã cho

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

4

Bạn cũng có thể chỉ định cách bạn muốn bộ sưu tập kết quả được khóa

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

5

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

22 cũng hỗ trợ truy xuất các giá trị lồng nhau bằng cách sử dụng ký hiệu "dấu chấm"

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

6

Nếu tồn tại các khóa trùng lặp, phần tử phù hợp cuối cùng sẽ được chèn vào bộ sưu tập đã chọn

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

7

$average = collect[[1, 1, 2, 4]]->avg[];

24

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

25 loại bỏ và trả về mục cuối cùng từ bộ sưu tập

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

8

Bạn có thể chuyển một số nguyên cho phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

25 để xóa và trả lại nhiều mục từ phần cuối của bộ sưu tập

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

8

$average = collect[[1, 1, 2, 4]]->avg[];

27

Phương pháp

$average = collect[[1, 1, 2, 4]]->avg[];

28 thêm một mục vào phần đầu của bộ sưu tập

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

8

Bạn cũng có thể chuyển đối số thứ hai để chỉ định khóa của mục được thêm vào trước

$collection = collect[[1, 2, 3]];

01

$average = collect[[1, 1, 2, 4]]->avg[];

29

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

30 loại bỏ và trả về một mục từ bộ sưu tập bằng khóa của nó

$collection = collect[[1, 2, 3]];

02

$average = collect[[1, 1, 2, 4]]->avg[];

31

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

32 nối thêm một mục vào cuối bộ sưu tập

$collection = collect[[1, 2, 3]];

7

$average = collect[[1, 1, 2, 4]]->avg[];

33

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

34 đặt khóa và giá trị đã cho trong bộ sưu tập

$collection = collect[[1, 2, 3]];

04

$average = collect[[1, 1, 2, 4]]->avg[];

35

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

36 trả về một mục ngẫu nhiên từ bộ sưu tập

$collection = collect[[1, 2, 3]];

05

Bạn có thể chuyển một số nguyên tới

$average = collect[[1, 1, 2, 4]]->avg[];

36 để chỉ định số lượng mục bạn muốn truy xuất ngẫu nhiên. Một bộ sưu tập các mặt hàng luôn được trả về khi chuyển rõ ràng số lượng mặt hàng bạn muốn nhận

$collection = collect[[1, 2, 3]];

06

Nếu thể hiện bộ sưu tập có ít mục hơn so với yêu cầu, phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

36 sẽ đưa ra một

$average = collect[[1, 1, 2, 4]]->avg[];

39

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

36 cũng chấp nhận một bao đóng, sẽ nhận được thể hiện bộ sưu tập hiện tại

$collection = collect[[1, 2, 3]];

07

$average = collect[[1, 1, 2, 4]]->avg[];

41

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

42 trả về một tập hợp chứa các số nguyên nằm trong phạm vi đã chỉ định

$collection = collect[[1, 2, 3]];

08

$average = collect[[1, 1, 2, 4]]->avg[];

43

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

44 giảm tập hợp thành một giá trị duy nhất, chuyển kết quả của mỗi lần lặp vào lần lặp tiếp theo

$collection = collect[[1, 2, 3]];

09

Giá trị của

$average = collect[[1, 1, 2, 4]]->avg[];

45 trong lần lặp đầu tiên là

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

09;

$collection = collect[[1, 2, 3]];

10

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

44 cũng chuyển các khóa mảng trong các bộ sưu tập kết hợp tới hàm gọi lại đã cho

$collection = collect[[1, 2, 3]];

11

$average = collect[[1, 1, 2, 4]]->avg[];

49

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

50 rút gọn bộ sưu tập thành một mảng các giá trị, chuyển kết quả của mỗi lần lặp vào lần lặp tiếp theo. Phương pháp này tương tự như phương pháp

$average = collect[[1, 1, 2, 4]]->avg[];

44;

$collection = collect[[1, 2, 3]];

12

$average = collect[[1, 1, 2, 4]]->avg[];

52

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

53 lọc bộ sưu tập bằng cách sử dụng bao đóng đã cho. Việc đóng sẽ trả lại

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56 nếu vật phẩm cần được xóa khỏi bộ sưu tập kết quả

$collection = collect[[1, 2, 3]];

13

Đối với nghịch đảo của phương pháp

$average = collect[[1, 1, 2, 4]]->avg[];

53, hãy xem phương pháp

$average = collect[[1, 1, 2, 4]]->avg[];

57

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

58 hoạt động tương tự như phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

99;

$collection = collect[[1, 2, 3]];

14

$average = collect[[1, 1, 2, 4]]->avg[];

61

Phương thức này hoạt động giống như

$average = collect[[1, 1, 2, 4]]->avg[];

58, nhưng nó sẽ lặp lại thành các mảng và áp dụng quy trình thay thế tương tự cho các giá trị bên trong

$collection = collect[[1, 2, 3]];

15

$average = collect[[1, 1, 2, 4]]->avg[];

63

Phương pháp

$average = collect[[1, 1, 2, 4]]->avg[];

64 đảo ngược thứ tự các mục của bộ sưu tập, giữ nguyên các khóa ban đầu

$collection = collect[[1, 2, 3]];

16

$average = collect[[1, 1, 2, 4]]->avg[];

65

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

66 tìm kiếm bộ sưu tập cho giá trị đã cho và trả về khóa của nó nếu tìm thấy. Nếu mặt hàng không được tìm thấy,

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

92 được trả lại

$collection = collect[[1, 2, 3]];

17

Việc tìm kiếm được thực hiện bằng cách so sánh "lỏng lẻo", nghĩa là một chuỗi có giá trị nguyên sẽ được coi là bằng một số nguyên có cùng giá trị. Để sử dụng phép so sánh "nghiêm ngặt", hãy chuyển

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56 làm đối số thứ hai cho phương thức

$collection = collect[[1, 2, 3]];

18

Ngoài ra, bạn có thể cung cấp cách đóng của riêng mình để tìm kiếm mục đầu tiên vượt qua bài kiểm tra độ tin cậy nhất định

$collection = collect[[1, 2, 3]];

19

$average = collect[[1, 1, 2, 4]]->avg[];

69

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

70 loại bỏ và trả về mục đầu tiên từ bộ sưu tập

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

8

Bạn có thể chuyển một số nguyên cho phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

70 để xóa và trả lại nhiều mục từ đầu bộ sưu tập

$collectionA = collect[[1, 2, 3]];

$collectionB = $collectionA->collect[];

8

$average = collect[[1, 1, 2, 4]]->avg[];

72

Phương pháp

$average = collect[[1, 1, 2, 4]]->avg[];

73 xáo trộn ngẫu nhiên các mục trong bộ sưu tập

$collection = collect[[1, 2, 3]];

22

$average = collect[[1, 1, 2, 4]]->avg[];

74

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

75 trả về một bộ sưu tập mới, với số lượng phần tử đã cho bị xóa khỏi phần đầu của bộ sưu tập

$collection = collect[[1, 2, 3]];

23

$average = collect[[1, 1, 2, 4]]->avg[];

76

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

77 bỏ qua các mục từ bộ sưu tập cho đến khi hàm gọi lại đã cho trả về

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56 và sau đó trả về các mục còn lại trong bộ sưu tập dưới dạng phiên bản bộ sưu tập mới

$collection = collect[[1, 2, 3]];

24

Bạn cũng có thể chuyển một giá trị đơn giản cho phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

77 để bỏ qua tất cả các mục cho đến khi tìm thấy giá trị đã cho

$collection = collect[[1, 2, 3]];

25

Cảnh báo
Nếu giá trị đã cho không được tìm thấy hoặc lệnh gọi lại không bao giờ trả về

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56, thì phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

77 sẽ trả về một bộ sưu tập trống

$average = collect[[1, 1, 2, 4]]->avg[];

82

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

83 bỏ qua các mục từ bộ sưu tập trong khi hàm gọi lại đã cho trả về

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56 và sau đó trả về các mục còn lại trong bộ sưu tập dưới dạng một bộ sưu tập mới

$collection = collect[[1, 2, 3]];

26

Cảnh báo
Nếu cuộc gọi lại không bao giờ trả về

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

92, phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

83 sẽ trả về một bộ sưu tập trống

$average = collect[[1, 1, 2, 4]]->avg[];

87

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

88 trả về một phần của bộ sưu tập bắt đầu từ chỉ mục đã cho

$collection = collect[[1, 2, 3]];

27

Nếu bạn muốn giới hạn kích thước của lát cắt được trả về, hãy chuyển kích thước mong muốn làm đối số thứ hai cho phương thức

$collection = collect[[1, 2, 3]];

28

Theo mặc định, lát cắt được trả về sẽ giữ nguyên các khóa. Nếu bạn không muốn giữ nguyên các khóa ban đầu, bạn có thể sử dụng phương pháp này để giới thiệu lại chúng

$average = collect[[1, 1, 2, 4]]->avg[];

90

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

91 trả về một tập hợp các khối mới biểu thị chế độ xem "cửa sổ trượt" của các mục trong bộ sưu tập

$collection = collect[[1, 2, 3]];

29

Điều này đặc biệt hữu ích khi kết hợp với phương pháp

$collection = collect[[1, 2, 3]];

30

Bạn có thể tùy chọn chuyển giá trị "bước" thứ hai, giá trị này xác định khoảng cách giữa mục đầu tiên của mỗi đoạn

$collection = collect[[1, 2, 3]];

31

$average = collect[[1, 1, 2, 4]]->avg[];

93

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

94 trả về phần tử đầu tiên trong bộ sưu tập vượt qua bài kiểm tra giá trị thực nhất định, nhưng chỉ khi bài kiểm tra giá trị thực khớp với chính xác một phần tử

$collection = collect[[1, 2, 3]];

32

You may also pass a key / value pair to the

$average = collect[[1, 1, 2, 4]]->avg[];

94 method, which will return the first element in the collection that matches the given pair, but only if it exactly one element matches

$collection = collect[[1, 2, 3]];

33

Ngoài ra, bạn cũng có thể gọi phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

94 không có đối số để lấy phần tử đầu tiên trong tập hợp nếu chỉ có một phần tử

$collection = collect[[1, 2, 3]];

34

Nếu không có phần tử nào trong bộ sưu tập cần được trả về bằng phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

94, một ngoại lệ

$average = collect[[1, 1, 2, 4]]->avg[];

98 sẽ được đưa ra. Nếu có nhiều hơn một phần tử cần được trả về, một

$average = collect[[1, 1, 2, 4]]->avg[];

99 sẽ được ném ra

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

00

Alias for the method

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

02

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

03 sắp xếp bộ sưu tập. Bộ sưu tập được sắp xếp giữ các khóa mảng ban đầu, vì vậy trong ví dụ sau, chúng tôi sẽ sử dụng phương pháp để đặt lại các khóa thành các chỉ mục được đánh số liên tiếp

$collection = collect[[1, 2, 3]];

35

Nếu nhu cầu sắp xếp của bạn nâng cao hơn, bạn có thể chuyển một cuộc gọi lại tới

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

03 bằng thuật toán của riêng bạn. Tham khảo tài liệu PHP trên , đây là cách gọi phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

03 của bộ sưu tập sử dụng nội bộ

Note
Nếu bạn cần sắp xếp một tập hợp các mảng hoặc đối tượng lồng nhau, hãy xem các phương thức và

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

10

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

08 sắp xếp bộ sưu tập theo khóa đã cho. Bộ sưu tập được sắp xếp giữ các khóa mảng ban đầu, vì vậy trong ví dụ sau, chúng tôi sẽ sử dụng phương pháp để đặt lại các khóa thành các chỉ mục được đánh số liên tiếp

$collection = collect[[1, 2, 3]];

36

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

08 chấp nhận các cờ sắp xếp làm đối số thứ hai của nó

$collection = collect[[1, 2, 3]];

37

Ngoài ra, bạn có thể chuyển bao đóng của riêng mình để xác định cách sắp xếp các giá trị của bộ sưu tập

$collection = collect[[1, 2, 3]];

38

Nếu bạn muốn sắp xếp bộ sưu tập của mình theo nhiều thuộc tính, bạn có thể chuyển một mảng các thao tác sắp xếp cho phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

08. Mỗi thao tác sắp xếp phải là một mảng bao gồm thuộc tính mà bạn muốn sắp xếp theo và hướng sắp xếp mong muốn

$collection = collect[[1, 2, 3]];

39

Khi sắp xếp một bộ sưu tập theo nhiều thuộc tính, bạn cũng có thể cung cấp các bao đóng xác định từng thao tác sắp xếp

$collection = collect[[1, 2, 3]];

40

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

15

Phương thức này có cùng chữ ký với phương thức, nhưng sẽ sắp xếp bộ sưu tập theo thứ tự ngược lại

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

17

Phương thức này sẽ sắp xếp bộ sưu tập theo thứ tự ngược lại với phương thức

$collection = collect[[1, 2, 3]];

41

Không giống như

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

03, bạn không được chuyển lệnh đóng cho

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

20. Thay vào đó, bạn nên sử dụng phương pháp và đảo ngược phép so sánh của mình

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

22

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

23 sắp xếp bộ sưu tập theo các khóa của mảng kết hợp cơ bản

$collection = collect[[1, 2, 3]];

42

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

24

Phương thức này có cùng chữ ký với phương thức, nhưng sẽ sắp xếp bộ sưu tập theo thứ tự ngược lại

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

26

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

27 sắp xếp bộ sưu tập theo các khóa của mảng kết hợp cơ bản bằng cách sử dụng hàm gọi lại

$collection = collect[[1, 2, 3]];

43

Hàm gọi lại phải là một hàm so sánh trả về một số nguyên nhỏ hơn, bằng hoặc lớn hơn 0. Để biết thêm thông tin, hãy tham khảo tài liệu PHP trên , đây là hàm PHP mà phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

27 sử dụng nội bộ

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

30

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

31 xóa và trả về một lát mục bắt đầu từ chỉ mục đã chỉ định

$collection = collect[[1, 2, 3]];

44

Bạn có thể chuyển đối số thứ hai để giới hạn kích thước của bộ sưu tập kết quả

$collection = collect[[1, 2, 3]];

45

Ngoài ra, bạn có thể chuyển đối số thứ ba chứa các mục mới để thay thế các mục đã bị xóa khỏi bộ sưu tập

$collection = collect[[1, 2, 3]];

46

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

32

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

33 chia một bộ sưu tập thành một số nhóm nhất định

$collection = collect[[1, 2, 3]];

47

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

34

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

35 chia một tập hợp thành một số nhóm nhất định, lấp đầy hoàn toàn các nhóm không đầu cuối trước khi phân bổ phần còn lại cho nhóm cuối cùng

$collection = collect[[1, 2, 3]];

48

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

36

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

37 trả về tổng của tất cả các mục trong bộ sưu tập

$collection = collect[[1, 2, 3]];

49

Nếu bộ sưu tập chứa các mảng hoặc đối tượng lồng nhau, bạn nên chuyển một khóa sẽ được sử dụng để xác định giá trị nào cần tính tổng

$collection = collect[[1, 2, 3]];

50

Ngoài ra, bạn có thể chuyển bao đóng của riêng mình để xác định giá trị nào của bộ sưu tập sẽ tính tổng

$collection = collect[[1, 2, 3]];

51

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

38

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

39 trả về một bộ sưu tập mới với số lượng mục được chỉ định

$collection = collect[[1, 2, 3]];

52

Bạn cũng có thể chuyển một số nguyên âm để lấy số mục đã chỉ định từ cuối bộ sưu tập

$collection = collect[[1, 2, 3]];

53

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

40

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

41 trả về các mục trong bộ sưu tập cho đến khi hàm gọi lại đã cho trả về

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56

$collection = collect[[1, 2, 3]];

54

Bạn cũng có thể chuyển một giá trị đơn giản cho phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

41 để lấy các mục cho đến khi tìm thấy giá trị đã cho

$collection = collect[[1, 2, 3]];

55

Cảnh báo
Nếu giá trị đã cho không được tìm thấy hoặc lệnh gọi lại không bao giờ trả về

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56, thì phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

41 sẽ trả về tất cả các mục trong bộ sưu tập

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

46

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

47 trả về các mục trong bộ sưu tập cho đến khi hàm gọi lại đã cho trả về

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

92

$collection = collect[[1, 2, 3]];

56

Cảnh báo
If the callback never returns

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

92, the

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

47 method will return all items in the collection

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

51

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

52 chuyển bộ sưu tập tới lệnh gọi lại đã cho, cho phép bạn "chạm" vào bộ sưu tập tại một điểm cụ thể và làm điều gì đó với các mục trong khi không ảnh hưởng đến chính bộ sưu tập đó. Bộ sưu tập sau đó được trả về bằng phương pháp

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

52

$collection = collect[[1, 2, 3]];

57

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

54

Phương thức tĩnh

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

55 tạo một bộ sưu tập mới bằng cách gọi bao đóng đã cho một số lần được chỉ định

$collection = collect[[1, 2, 3]];

58

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

56

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

57 chuyển đổi bộ sưu tập thành một PHP

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

44 đơn giản. Nếu các giá trị của bộ sưu tập là các mô hình Eloquent, thì các mô hình đó cũng sẽ được chuyển thành mảng

$collection = collect[[1, 2, 3]];

59

Cảnh báo

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

57 cũng chuyển đổi tất cả các đối tượng lồng nhau của bộ sưu tập là một thể hiện của

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

60 thành một mảng. Nếu bạn muốn lấy mảng thô bên dưới bộ sưu tập, thay vào đó hãy sử dụng phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

62

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

63 chuyển đổi bộ sưu tập thành một chuỗi được tuần tự hóa JSON

$collection = collect[[1, 2, 3]];

60

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

64

Phương thức

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

85 lặp lại bộ sưu tập và gọi hàm gọi lại đã cho với từng mục trong bộ sưu tập. Các mục trong bộ sưu tập sẽ được thay thế bằng các giá trị được trả về bởi lệnh gọi lại

$collection = collect[[1, 2, 3]];

61

Cảnh báo
Không giống như hầu hết các phương pháp thu thập khác,

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

85 tự sửa đổi bộ sưu tập. Thay vào đó, nếu bạn muốn tạo một bộ sưu tập mới, hãy sử dụng phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

68

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

69 mở rộng tập hợp một chiều sử dụng ký hiệu "dấu chấm" thành tập hợp nhiều chiều

$collection = collect[[1, 2, 3]];

62

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

70

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

71 thêm mảng đã cho vào bộ sưu tập. Nếu mảng đã cho chứa các khóa đã có trong bộ sưu tập ban đầu, thì các giá trị của bộ sưu tập ban đầu sẽ được ưu tiên

$collection = collect[[1, 2, 3]];

63

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

72

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

73 trả về tất cả các mục duy nhất trong bộ sưu tập. Bộ sưu tập được trả về giữ các khóa mảng ban đầu, vì vậy trong ví dụ sau, chúng tôi sẽ sử dụng phương pháp để đặt lại các khóa thành các chỉ mục được đánh số liên tiếp

$collection = collect[[1, 2, 3]];

64

Khi xử lý các mảng hoặc đối tượng lồng nhau, bạn có thể chỉ định khóa được sử dụng để xác định tính duy nhất

$collection = collect[[1, 2, 3]];

65

Cuối cùng, bạn cũng có thể chuyển bao đóng của riêng mình cho phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

73 để chỉ định giá trị nào sẽ xác định tính duy nhất của một mục

$collection = collect[[1, 2, 3]];

66

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

73 sử dụng so sánh "lỏng lẻo" khi kiểm tra giá trị mục, nghĩa là một chuỗi có giá trị nguyên sẽ được coi là bằng với một số nguyên có cùng giá trị. Sử dụng phương pháp để lọc bằng cách so sánh "nghiêm ngặt"

Note
This method's behavior is modified when using

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

78

This method has the same signature as the method; however, all values are compared using "strict" comparisons

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

80

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

81 sẽ thực hiện cuộc gọi lại đã cho trừ khi đối số đầu tiên được cung cấp cho phương thức ước tính là

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56

$collection = collect[[1, 2, 3]];

67

Cuộc gọi lại thứ hai có thể được chuyển đến phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

81. Cuộc gọi lại thứ hai sẽ được thực thi khi đối số đầu tiên được cung cấp cho phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

81 đánh giá là

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56

$collection = collect[[1, 2, 3]];

68

Đối với nghịch đảo của

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

81, hãy xem phương pháp

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

88

Alias for the method

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

90

Alias for the method

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

92

Phương thức tĩnh

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

93 trả về các mục cơ bản của bộ sưu tập từ giá trị đã cho khi áp dụng

$collection = collect[[1, 2, 3]];

69

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

94

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

95 lấy một giá trị đã cho từ phần tử đầu tiên của tập hợp

$collection = collect[[1, 2, 3]];

70

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

96

Phương thức

$average = collect[[1, 1, 2, 4]]->avg[];

89 trả về một bộ sưu tập mới với các khóa được đặt lại thành các số nguyên liên tiếp

$collection = collect[[1, 2, 3]];

71

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

98

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

87 sẽ thực hiện lệnh gọi lại đã cho khi đối số đầu tiên được cung cấp cho phương thức ước tính là

use Illuminate\Support\Collection;

use Illuminate\Support\Facades\Lang;

Collection::macro['toLocale', function [$locale] {

return $this->map[function [$value] use [$locale] {

return Lang::get[$value, [], $locale];

$collection = collect[['first', 'second']];

$translated = $collection->toLocale['es'];

56. Thể hiện bộ sưu tập và đối số đầu tiên được cung cấp cho phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

87 sẽ được cung cấp cho phương thức đóng

$collection = collect[[1, 2, 3]];

72

Cuộc gọi lại thứ hai có thể được chuyển đến phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

87. Cuộc gọi lại thứ hai sẽ được thực thi khi đối số đầu tiên được cung cấp cho phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

87 đánh giá là

use Illuminate\Support\Collection;

use Illuminate\Support\Str;

Collection::macro['toUpper', function [] {

return $this->map[function [$value] {

return Str::upper[$value];

$collection = collect[['first', 'second']];

$upper = $collection->toUpper[];

92

$collection = collect[[1, 2, 3]];

73

Đối với nghịch đảo của

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

87, hãy xem phương pháp

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

07

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

91 sẽ thực hiện lệnh gọi lại đã cho khi bộ sưu tập trống

$collection = collect[[1, 2, 3]];

74

Lần đóng thứ hai có thể được chuyển đến phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

91 sẽ được thực thi khi bộ sưu tập không trống

$collection = collect[[1, 2, 3]];

75

Đối với nghịch đảo của

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

91, hãy xem phương pháp

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

12

Phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

89 sẽ thực hiện lệnh gọi lại đã cho khi bộ sưu tập không trống

$collection = collect[[1, 2, 3]];

76

Lần đóng thứ hai có thể được chuyển đến phương thức

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

89 sẽ được thực thi khi bộ sưu tập trống

$collection = collect[[1, 2, 3]];

77

Đối với nghịch đảo của

$collection = collect[[1, 2, 3, 4, 5, 6, 7]];

$chunks = $collection->chunk[4];

// [[1, 2, 3, 4], [5, 6, 7]]

89, hãy xem phương pháp

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

17

Phương thức

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

18 lọc bộ sưu tập theo một cặp khóa/giá trị nhất định

$collection = collect[[1, 2, 3]];

78

Phương pháp

@foreach [$products->chunk[3] as $chunk]

@foreach [$chunk as $product]

{{ $product->name }}

18 sử dụng so sánh "lỏng lẻo" khi kiểm tra giá trị mục, nghĩa là một chuỗi có giá trị nguyên sẽ được coi là bằng với một số nguyên có cùng giá trị. Sử dụng phương pháp để lọc bằng cách so sánh "nghiêm ngặt"

Optionally, you may pass a comparison operator as the second parameter. Supported operators are: '===', '!==', '!=', '==', '=', '', '>', '=', and 'key in the controller or in the view . Chỉ mục thường là một số nguyên và khóa là thứ bạn đang muốn trích xuất.

How to iterate through Collection in Laravel?

All collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays. .
$users = App\User. where['active', 1]->get[];
foreach [$user là $user] {
echo $user->name;

Chủ Đề