Làm cách nào để có được 10 bản ghi đầu tiên trong Laravel?

Mệnh đề LIMIT giúp dễ dàng mã hóa kết quả nhiều trang hoặc phân trang bằng SQL và rất hữu ích trên các bảng lớn. Trả lại một số lượng lớn các bản ghi có thể ảnh hưởng đến hiệu suất

Giả sử chúng tôi muốn chọn tất cả các bản ghi từ 1 - 30 (bao gồm) từ một bảng có tên "Đơn hàng". Truy vấn SQL sau đó sẽ trông như thế này

Trong hướng dẫn nhanh này, bạn sẽ học cách lấy 1 bản ghi cuối cùng, 5 bản ghi cuối cùng, 10 bản ghi cuối cùng hoặc n số bản ghi trong laravel

Chúng ta sẽ thảo luận về 2 cách lấy các bản ghi cuối cùng bằng cách sử dụng hàm eloquent latest()orderBy() trong laravel

Ngoài ra hướng dẫn này cũng hợp lệ cho laravel 6, laravel 7, laravel 8 và laravel 9

Vậy hãy bắt đầu

Mục lục

Giả thiết

Giả sử rằng tôi có Mô hình Article được gắn với bảng article

Làm cách nào để lấy 1 bản ghi cuối cùng của bảng trong Laravel?

Có hai phương pháp để lấy bản ghi cuối cùng của bảng

a) Sử dụng mới nhất()

Trong phần này, chúng ta sẽ sử dụng latest(). Nhập mã sau vào bộ điều khiển của bạn

Article::latest()->first();

Trong đoạn mã trên, latest() là phương thức sắp xếp dữ liệu

Article::orderBy('created_at','desc')->first();
1 dựa trên cột
Article::orderBy('created_at','desc')->first();
2. Và
Article::orderBy('created_at','desc')->first();
3 được sử dụng để lấy hàng đầu tiên. Vì vậy, điều này sẽ nhận được 1 bản ghi cuối cùng của bảng

b) Sử dụng OrderBy()

Chúng tôi cũng có thể tìm nạp phương thức sử dụng orderBy() để lấy bản ghi cuối cùng từ bảng

Article::orderBy('created_at','desc')->first();

Trong đoạn mã trên, giống như latest(), chúng tôi đang áp dụng

Article::orderBy('created_at','desc')->first();
6 trên
Article::orderBy('created_at','desc')->first();
2 và sắp xếp nó trên
Article::orderBy('created_at','desc')->first();
1, sau đó lấy hàng đầu tiên bằng cách sử dụng phương pháp
Article::orderBy('created_at','desc')->first();
3, hàng cuối cùng sẽ là hàng cuối cùng kể từ khi chúng tôi sắp xếp giảm dần

Làm cách nào để lấy 5 bản ghi cuối cùng của bảng trong Laravel?

Chúng tôi sẽ sử dụng orderBy()

Article::orderBy('created_at','desc')->take(5)->get();
1 để lấy 5 bản ghi cuối cùng của bảng trong laravel

Article::orderBy('created_at','desc')->take(5)->get();

Trong đoạn mã trên, đầu tiên chúng tôi sắp xếp thứ tự bằng cách giảm dần trên cột

Article::orderBy('created_at','desc')->first();
2, sau đó chúng tôi sử dụng phương pháp
Article::orderBy('created_at','desc')->take(5)->get();
1 trong đó chúng tôi chỉ định số lượng hàng chúng tôi muốn. Vì chúng tôi muốn 5, đó là lý do tại sao chúng tôi đề cập đến 5 như đối số

Làm cách nào để lấy 10 bản ghi cuối cùng của bảng trong Laravel?

Tương tự với 5 bản ghi cuối cùng, chúng tôi sẽ sử dụng lại orderBy()

Article::orderBy('created_at','desc')->take(5)->get();
1 để lấy 10 bản ghi cuối cùng từ bảng cơ sở dữ liệu trong laravel

Article::orderBy('created_at','desc')->take(10)->get();

Đoạn mã trên trước tiên sẽ sắp xếp các hàng theo cột

Article::orderBy('created_at','desc')->first();
2 theo thứ tự giảm dần và sẽ lấy 10 bản ghi bằng cách sử dụng
Article::orderBy('created_at','desc')->take(5)->get();
7

Làm cách nào để lấy N bản ghi hoặc mục nhập cuối cùng của bảng trong Laravel?

Để lấy

Article::orderBy('created_at','desc')->take(5)->get();
8 bản ghi cuối cùng từ bảng trong laravel, một lần nữa chúng ta sử dụng ________ 658 _______ và
Article::orderBy('created_at','desc')->take(5)->get();
1

$n=50;
Article::orderBy('created_at','desc')->take($n)->get();

Trong đoạn mã trên, chúng tôi đã khai báo

Article::orderBy('created_at','desc')->take(5)->get();
8 một biến có thể là bất kỳ số nào bạn muốn lấy các hàng cuối cùng của. Vì vậy, trong trường hợp của tôi, tôi muốn tìm nạp 50 bản ghi cuối cùng, vì vậy tại sao tôi đặt
Article::orderBy('created_at','desc')->take(5)->get();
8 thành 50

Phần kết luận

Hi vọng bài viết trên bạn đã biết cách lấy 1, 5, 10 hoặc n số lượng entry/record cuối cùng trong laravel

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

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

Tạo bộ sưu tập

Như đã đề cập ở trên, 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 trả về một thể hiện

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

99 mới cho mảng đã cho. Vì vậy, việc tạo một bộ sưu tập cũng đơn giản như

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

Ghi chú
Kết quả của các truy vấn Eloquent luôn được trả về dưới dạ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();

02 trường hợp

Mở rộng bộ sưu tập

Các bộ sưu tập là "macroable", cho phép bạn thêm các phương thức bổ sung 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. 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();

10 của lớp

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

99 chấp nhận một bao đóng sẽ được thực thi khi macro của bạn được gọi. Việc đóng macro có thể truy cập các phương thức khác của bộ sưu tập thông qua

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, giống như thể nó là một phương thức thực sự của lớp bộ sưu tập. Ví dụ: đoạn mã sau thêm 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();

12 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

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

Thông thường, bạn nên khai báo các macro thu thập theo 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();

14 của nhà cung cấp dịch vụ

Đối số vĩ mô

Nếu cần, bạn có thể xác định các macro chấp nhận các đối số bổ sung

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

phương pháp có sẵn

Đối với phần lớn các tài liệu về bộ sưu tập còn lại, chúng ta sẽ thảo luận về từng phương thức có sẵn trên 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. Hãy nhớ rằng, tất cả các phương thức này có thể được xâu chuỗi để thao tác thành thạo mảng bên dưới. Hơn nữa, hầu hết mọi phương thức đều trả về một phiên bản

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 mới, cho phép bạn giữ lại bản gốc của bộ sưu tập khi cần thiết

Danh sách 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();

17

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

18 trả về mảng cơ bản được đại diện bởi tập hợ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();

19

Bí danh cho 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();

21

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

20 trả về giá trị trung bình của một khóa đã cho

$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

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

24 chia bộ sưu tập thành nhiều bộ sưu tập nhỏ hơn với kích thước nhất định

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

$chunks = $collection->chunk(4);

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

Phương pháp này đặc biệt hữu ích trong các khung nhìn khi làm việc với hệ thống lưới như Bootstrap. Ví dụ: hãy tưởng tượng bạn có một bộ sưu tập các mô hình Eloquent mà bạn muốn hiển thị dưới dạng lưới

@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

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

26 chia bộ sưu tập thành nhiều bộ sưu tập nhỏ hơn dựa trên đánh giá của cuộc gọi lại đã cho. Biến

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 được truyền cho bao đóng có thể được sử dụng để kiểm tra phần tử trướ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']]

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

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

29 thu gọn một tập hợp các mảng thành một tập hợp phẳng duy nhất

$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

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

00 trả về một phiên bản

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 mới với các mục hiện có trong bộ sưu tập

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

$collectionB = $collectionA->collect();

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

00 chủ yếu hữu ích để chuyển đổi thành các phiên bản

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 tiêu chuẩn

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

0

Ghi chú
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();

00 đặc biệt hữu ích khi bạn có một phiên bản 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();

36 và cần một phiên bản bộ sưu tập không lười biếng. 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();

30 là một phần của hợp đồ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();

36, nên bạn có thể sử dụng nó một cách an toàn để lấy phiên bản

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

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

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

41 kết hợp các giá trị của bộ sưu tập, dưới dạng khóa, với các giá trị của một mảng hoặc bộ sưu tập khác

$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

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

43 nối các giá trị 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();

44 hoặc bộ sưu tập đã cho vào phần cuối của bộ sưu tập khác

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

2

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

43 lập chỉ mục lại các khóa bằng số cho các mục được nối vào bộ sưu tập gốc. Để duy trì các khóa trong các bộ sưu tập kết hợp, hãy xem 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();

46

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

47 xác định xem bộ sưu tập có 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();

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

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

3

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

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

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

4

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

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

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

5

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

47 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"

Đối với nghịch đảo 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();

47, hãy xem 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();

54

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

55 xác định xem bộ sưu tập có chứa một mục duy nhất hay không

$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

Phương thức này có cùng chữ ký với phương thức;

Ghi chú
Hành vi của phương pháp này được sửa đổi khi sử dụ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();

58

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

59 trả về tổng số mục trong bộ sưu tập

$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

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

61 đếm số lần xuất hiện của các giá trị trong tập hợp. Theo mặc định, phương thức đếm số lần xuất hiện của mọi phần tử, cho phép bạn đếm một số "loại" phần tử nhất định trong bộ sưu tập

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

8

Bạn chuyển một lần đó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();

61 để đếm tất cả các mục theo một giá trị tùy chỉnh

$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

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

64 nối chéo các giá trị của tập hợp giữa các mảng hoặc tập hợp đã cho, trả về một tích Descartes với tất cả các hoán vị có 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();

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

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

66 kết xuất các mục của bộ sưu tập và kết thúc thực thi tập lệ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();

1

Nếu bạn không muốn dừng thực thi tập lệnh, hãy sử dụng phương pháp 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();

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ó. Phương thức này sẽ trả về các giá trị trong bộ sưu tập ban đầu không có trong bộ sưu tập đã cho

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

Ghi chú
Hành vi của phương pháp này được sửa đổi khi sử dụ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();

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 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 khóa và giá trị của nó. Phương thức này sẽ trả về các cặp khóa/giá trị trong bộ sưu tập ban đầu không có trong bộ sưu tập đã cho

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

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

75 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 khóa của nó. Phương thức này sẽ trả về các cặp khóa/giá trị trong bộ sưu tập ban đầu không có trong bộ sưu tập đã cho

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 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 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

Nếu bộ sưu tập chứa các mảng hoặc đối tượng, bạn có thể chuyển khóa của các thuộc tính mà bạn muốn kiểm tra các giá trị trùng lặ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');

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

Phương thức này có cùng chữ ký vớ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();

90

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

91 lặp lại các mục trong bộ sưu tập và chuyển từng mục đến một bao đó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');

1

Nếu bạn muốn ngừng lặp qua các mục, bạn có thể quay lại _______23_______92 từ lần đóng của mì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');

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

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

94 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 lệnh gọi lại đã cho

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

Bạn có thể ngừng lặp qua các mục bằng cách 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 từ cuộc gọi 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');

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

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

97 có thể được sử dụng để xác minh rằng tất cả các yếu tố của bộ sưu tập đều vượt qua một bài kiểm tra sự thật nhấ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');

5

Nếu bộ sưu tập trống, 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();

97 sẽ trả về 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

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

00 trả về tất cả các mục trong bộ sưu tập ngoại trừ những mục có khóa được chỉ đị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');

7

Đối với nghịch đảo của

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, hãy xem phương pháp

Ghi chú
Hành vi của phương pháp này được sửa đổi khi sử dụ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');

02

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

03 lọc bộ sưu tập bằng cách sử dụng lệnh gọi lại đã cho, chỉ giữ lại những mục vượt qua bài kiểm tra độ tin cậy nhấ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');

8

Nếu không cung cấp lệnh gọi lại, tất cả các mục nhập của bộ sưu tập tương đương với

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 sẽ bị xóa

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

Đối với nghịch đảo của

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, hãy xem 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');

06

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

07 trả về phần tử đầu tiên trong bộ sưu tập vượt qua bài kiểm tra sự thật đã cho

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

0

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

07 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,

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ề

$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

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

11 giống với 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');

07;

$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 mục và trả lại, do đó tạo thành một bộ sưu tập mới gồm các mục đã 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 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 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, việc 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 kết quả 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');

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

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 tập hợ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 tập hợ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

Ghi chú
Hành vi của phương pháp này được sửa đổi khi sử dụ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');

51

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

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 sự thật đã cho

$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 phiên bản thu thậ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 mục và trả lại, 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']]

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

Ghi chú
Hành vi của phương pháp này được sửa đổi khi sử dụng

$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 đầu 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 pháp

$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 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. The closure should return

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 if the item should be removed from the resulting collection

$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 không tìm thấy mặt hà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();

92 sẽ đượ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ả về 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 không tìm thấy giá trị đã cho hoặc hàm 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 đại diện cho 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 cho trước, 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

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

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

94, phương thức này sẽ trả về phần tử đầu tiên trong bộ sưu tập khớp với cặp đã cho, nhưng chỉ khi nó khớp chính xác với một phần tử

$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ề, thì 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

Bí danh cho phương pháp

$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ộ

Ghi chú
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 không tìm thấy giá trị đã cho 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, 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
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

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

$chunks = $collection->chunk(4);

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

47 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]]

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à thực hiện đ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. If the collection's values are Eloquent models, the models will also be converted to arrays

$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. If you want to get the raw array underlying the collection, use the method instead

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

$chunks = $collection->chunk(4);

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

62

The

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

$chunks = $collection->chunk(4);

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

63 method converts the collection into a JSON serialized string

$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. The items in the collection will be replaced by the values returned by the callback

$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

The

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

$chunks = $collection->chunk(4);

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

69 method expands a single-dimensional collection that uses "dot" notation into a multi-dimensional collection

$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 pháp

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

$chunks = $collection->chunk(4);

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

73 sử dụng phép 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 the method to filter using "strict" comparisons

Ghi chú
Hành vi của phương pháp này được sửa đổi khi sử dụng

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

$chunks = $collection->chunk(4);

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

78

Phương thức này có cùng chữ ký vớ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]]

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

Bí danh cho phương pháp

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

$chunks = $collection->chunk(4);

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

90

Bí danh cho phương pháp

$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

The

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

$chunks = $collection->chunk(4);

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

87 method will execute the given callback when the first argument given to the method evaluates to

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. The collection instance and the first argument given to the

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

$chunks = $collection->chunk(4);

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

87 method will be provided to the closure

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

72

Một cuộc gọi lại thứ hai có thể được chuyển đến phương pháp

$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

A second closure may be passed to the

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

$chunks = $collection->chunk(4);

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

89 method that will be executed when the collection is empty

$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 phép 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 '<=':

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

79

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

21

Phương thức này có cùng chữ ký với phương thức;

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

23

Phương pháp

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

24 lọc bộ sưu tập bằng cách xác định xem một giá trị mục được chỉ định có nằm trong một phạm vi nhất định hay không

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

80

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

25

The

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

26 method removes elements from the collection that do not have a specified item value that is contained within the given array

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

81

Phương pháp

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

26 sử dụng phép 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"

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

29

Phương thức này có cùng chữ ký với phương thức;

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

31

Phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

32 lọc bộ sưu tập theo một loại lớp nhất định

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

82

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

33

Phương pháp

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

34 lọc bộ sưu tập bằng cách xác định xem một giá trị mục được chỉ định có nằm ngoài một phạm vi nhất định hay không

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

83

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

35

Phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

36 loại bỏ các phần tử khỏi bộ sưu tập có giá trị mục được chỉ định được chứa trong mảng đã cho

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

84

Phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

36 sử dụng phép 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"

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

39

Phương thức này có cùng chữ ký với phương thức;

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

41

Phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

42 trả về các mục từ bộ sưu tập mà khóa đã cho không phả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');

09

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

85

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

44

Phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

45 trả về các mục từ bộ sưu tập có khóa đã cho 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]);

86

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

47

Phương thức tĩnh

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

48 bao bọc giá trị đã cho trong một bộ sưu tập khi áp dụng

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

87

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

49

Phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

50 hợp nhất các giá trị của mảng đã cho với các giá trị của bộ sưu tập ban đầu tại chỉ mục tương ứng của chúng

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

88

Higher Order Messages

Bộ sưu tập cũng cung cấp hỗ trợ cho "thông báo bậc cao hơn", là lối tắt để thực hiện các tác vụ phổ biến trên bộ sưu tập. Các phương pháp thu thập cung cấp thông báo bậc cao hơn là. , , , , , , , , , , , , , , , , , , , , , , , và

Mỗi thông báo bậc cao hơn có thể được truy cập dưới dạng thuộc tính động trên phiên bản bộ sưu tập. Chẳng hạn, hãy sử dụng thông báo thứ tự cao hơn 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();

91 để gọi một phương thức trên mỗi đối tượng trong một bộ sưu tập

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

89

Tương tự như vậy, chúng ta có thể sử dụng thông báo thứ tự cao hơn của

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

$chunks = $collection->chunk(4);

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

37 để thu thập tổng số "phiếu bầu" cho một tập hợp người dùng

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

90

bộ sưu tập lười biếng

Giới thiệu

Cảnh báo
Trước khi tìm hiểu thêm về lazy collections của Laravel, hãy dành chút thời gian để làm quen với các trình tạo PHP

Để bổ sung cho 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 vốn đã mạnh mẽ, lớ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');

74 tận dụng các trình tạo của PHP để cho phép bạn làm việc với các tập dữ liệu rất lớn trong khi vẫn giữ mức sử dụng bộ nhớ thấp

Ví dụ: hãy tưởng tượng ứng dụng của bạn cần xử lý tệp nhật ký nhiều gigabyte trong khi tận dụng các phương thức thu thập của Laravel để phân tích nhật ký. Thay vì đọc toàn bộ tệp vào bộ nhớ cùng một lúc, bộ sưu tập lười biếng có thể được sử dụng để chỉ giữ một phần nhỏ của tệp trong bộ nhớ tại một thời điểm nhất định

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

91

Hoặc, hãy tưởng tượng bạn cần lặp qua 10.000 mô hình Eloquent. Khi sử dụng các bộ sưu tập truyền thống của Laravel, tất cả 10.000 mô hình Eloquent phải được tải vào bộ nhớ cùng một lúc

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

92

Tuy nhiên, phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

79 của trình tạo truy vấn trả về một thể hiệ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');

74. Điều này cho phép bạn vẫn chỉ chạy một truy vấn đối với cơ sở dữ liệu nhưng cũng chỉ giữ một mô hình Eloquent được tải trong bộ nhớ tại một thời điểm. Trong ví dụ này, cuộc gọi 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');

03 không được thực thi cho đến khi chúng tôi thực sự lặp lại từng người dùng riêng lẻ, cho phép giảm đáng kể mức sử dụng bộ nhớ

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

93

Tạo bộ sưu tập lười biếng

Để tạo một phiên bản bộ sưu tập lười biếng, bạn nên chuyển một hàm trình tạo PHP 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');

81 của bộ sưu tập

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

94

Hợp đồng vô số

Hầu như tất cả các phương thức có sẵn trên 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 cũng có sẵn trên lớ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');

74. Cả hai lớp này đều triển khai hợp đồng

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

85, trong đó xác định các phương thức sau

Cảnh báo
Các phương thức thay đổi bộ sưu tập (chẳng hạn như

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

70,

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

25,

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

28, v.v. ) không có trên lớ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');

74

Phương pháp thu thập lười biếng

Ngoài các phương thức được định nghĩa trong hợp đồ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();

36, lớ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');

74 chứa các phương thức sau

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

92

Phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

93 trả về một bộ sưu tập lười biếng mới sẽ liệt kê các giá trị cho đến thời điểm đã chỉ định. Sau thời gian đó, bộ sưu tập sẽ ngừng liệt kê

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

95

Để minh họa cách sử dụng phương pháp này, hãy tưởng tượng một ứng dụng gửi hóa đơn từ cơ sở dữ liệu bằng con trỏ. Bạn có thể xác định một tác vụ đã lên lịch chạy 15 phút một lần và chỉ xử lý hóa đơn trong tối đa 14 phút

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

96

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

94

Trong khi 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();

91 gọi hàm gọi lại đã cho cho từng mục trong bộ sưu tập ngay lập tức, thì phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

96 chỉ gọi hàm gọi lại đã cho khi các mục đang được lấy ra khỏi danh sách từng cái một

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

97

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

97

Phương thức

@foreach ($products->chunk(3) as $chunk)

@foreach ($chunk as $product)

{{ $product->name }}

98 trả về một bộ sưu tập lười biếng mới sẽ ghi nhớ bất kỳ giá trị nào đã được liệt kê và sẽ không truy xuất lại chúng trong các lần liệt kê bộ sưu tập tiếp theo

Làm cách nào để có được 10 hàng đầu tiên trong laravel?

Trước tiên, bạn có thể sử dụng Trình phân trang . Điều này đơn giản như. $allUsers = Người dùng. phân trang (15); . where('phiếu', '>', 100)->phân trang(15);

Làm cách nào để có được 10 bản ghi trong laravel?

chúng ta sẽ sử dụng latest() và orderBy() với phương thức take() hùng hồn để lấy 10 bản ghi cuối cùng từ cơ sở dữ liệu trong laravel.

Làm cách nào để lấy bản ghi đầu tiên trong laravel?

Sử dụng Laravel Eloquent, bạn có thể lấy một hàng sử dụng phương thức first() , nó trả về hàng đầu tiên của bảng nếu điều kiện where() không phù hợp .

Làm cách nào để có được các hàng cụ thể trong laravel?

Làm cách nào để lấy một hàng từ cơ sở dữ liệu trong Laravel? .
ví dụ 1. Sử dụng find() $userID = 1;.
ví dụ 2. Sử dụng firstWhere() $email = 'aatmaninfotech@gmail. com';.
ví dụ 3. Sử dụng where() và first() $email = 'aatmaninfotech@gmail. com';.
Ví dụ 4. Sử dụng đầu tiên() $user = Người dùng. đầu tiên();.
Ví dụ 5. Sử dụng take() và get()