😄Collections full (ok)

https://laravel.com/docs/9.x/collections#creating-collections

Bài tập 1: Xóa phần tử rỗng khỏi mảng

['taylor', 'abigail', null]

C:\xampp\htdocs\wpclidemo\app\Http\Controllers\HomeController.php

$collection = collect(['taylor', 'abigail', null])->map(function ($name) {
    return strtoupper($name);
  })->reject(function ($name) {
    return empty($name);
  });
  echo '<pre>';
    var_export($collection);
  echo '</pre>';
  die(" ");

Bài 2.1 : Extending Collectionsarrow-up-right

C:\xampp\htdocs\wpclidemo\app\Http\Controllers\HomeController.php

Thông thường, bạn nên khai báo macro thu thập trong phương pháp khởi động của nhà cung cấp dịch vụ. https://laravel.com/docs/9.x/providersarrow-up-right

Bài 2.2 Macro Argumentsarrow-up-right (Nếu cần, bạn có thể xác định các macro chấp nhận các đối số bổ sung:)

all(arrow-up-right)

avg()arrow-up-right

chunk()arrow-up-right

Thực hiện với view 👇

C:\xampp\htdocs\wpclidemo\app\Http\Controllers\HomeController.php

C:\xampp\htdocs\wpclidemo\resources\views\welcome.blade.php

chunkWhile()arrow-up-right

Result 👇

collapse()arrow-up-right

Result 👇

collect()arrow-up-right

Result 👇

combine()arrow-up-right

Result 👇

concat()arrow-up-right

Result 👇

contains()arrow-up-right

containsStrict()arrow-up-right This method has the same signature as the containsarrow-up-right method; however, all values are compared using "strict" comparisons.

Result 👇

Hoặc

Hoặc

Result 👇

count()arrow-up-right

Result 👇

countBy()arrow-up-right

crossJoin()arrow-up-right

diff()arrow-up-right

diffAssoc()arrow-up-right

diffKeys()arrow-up-right

doesntContain()arrow-up-right

duplicates()arrow-up-right

duplicatesStrict()arrow-up-right This method has the same signature as the duplicatesarrow-up-right method; however, all values are compared using "strict" comparisons.

each()arrow-up-right

eachSpread()arrow-up-right

every()arrow-up-right

😂If the collection is empty, the every method will return true:

except()arrow-up-right

filter()arrow-up-right

If no callback is supplied, all entries of the collection that are equivalent to false will be removed:

😒 For the inverse of filter, see the rejectarrow-up-right method.

reject()arrow-up-right

first()arrow-up-right

firstOrFail()arrow-up-right

firstWhere()arrow-up-right

flatMap()arrow-up-right

flatten()arrow-up-right

Hoặc

flip()arrow-up-right

forget()arrow-up-right

forPage()arrow-up-right

get()arrow-up-right

You may optionally pass a default value as the second argument:

You may even pass a callback as the method's default value. The result of the callback will be returned if the specified key does not exist:

groupBy()arrow-up-right

Hoặc

Hoặc

has()arrow-up-right

implode()arrow-up-right

Hoặc

intersect()arrow-up-right

intersectByKeys()arrow-up-right

isEmpty()arrow-up-right

isNotEmpty()arrow-up-right

join()arrow-up-right

keyBy()arrow-up-right

Hoặc

keys()arrow-up-right

last()arrow-up-right

You may also call the last method with no arguments to get the last element in the collection. If the collection is empty, null is returned:

lazy()arrow-up-right

macro()arrow-up-right

make()arrow-up-right

map()arrow-up-right

mapInto()arrow-up-right

mapSpread()arrow-up-right

mapToGroups()arrow-up-right

mapWithKeys()arrow-up-right

max()arrow-up-right

median()arrow-up-right

merge()arrow-up-right

Hoặc

mergeRecursive()arrow-up-right

min()arrow-up-right

mode()arrow-up-right

nth()arrow-up-right

Hoặc

only()arrow-up-right

pad()arrow-up-right

partition()arrow-up-right

pipe()arrow-up-right

pipeInto()arrow-up-right

pipeThrough()arrow-up-right

pluck()arrow-up-right

Hoặc

Hoặc

Hoặc

pop()arrow-up-right

Hoặc

prepend()arrow-up-right

Hoặc

pull()arrow-up-right

push()arrow-up-right

put()arrow-up-right

random()arrow-up-right

Hoặc

range()arrow-up-right

reduce()arrow-up-right

Hoặc

Hoặc

Chú ý 😒😒😒

Result

reduceSpread()arrow-up-right?? chưa hiểu??? 😒😒

reject()arrow-up-right

replace()arrow-up-right

replaceRecursive()arrow-up-right

reverse()arrow-up-right

search()arrow-up-right

Hoặc

Hoặc

shift()arrow-up-right

Hoặc

shuffle()arrow-up-right

skip()arrow-up-right

skipUntil()arrow-up-right

Hoặc

skipWhile()arrow-up-right

slice()arrow-up-right

Hoặc

sliding()arrow-up-right

Hoặc

sole()arrow-up-right

Hoặc

Hoặc

sort()arrow-up-right

sortBy()arrow-up-right

Hoặc

Hoặc

Hoặc

Hoặc

sortDesc()arrow-up-right

sortKeys()arrow-up-right

sortKeysUsing()arrow-up-right

splice()arrow-up-right

Hoặc

split()arrow-up-right

splitIn()arrow-up-right

sum()arrow-up-right

Hoặc

Hoặc

take()arrow-up-right

Hoặc

takeUntil()arrow-up-right

Hoặc

takeWhile()arrow-up-right

times()arrow-up-right

toArray()arrow-up-right

toJson()arrow-up-right

transform()arrow-up-right

undot()arrow-up-right

union()arrow-up-right

unique()arrow-up-right

Hoặc

Hoặc

unless()arrow-up-right

Hoặc

unwrap()arrow-up-right

value()arrow-up-right

values()arrow-up-right

when()arrow-up-right

Hoặc

whenEmpty()arrow-up-right

Hoặc

whenNotEmpty()arrow-up-right

where()arrow-up-right

Hoặc

whereBetween()arrow-up-right

whereIn()arrow-up-right

whereInstanceOf()arrow-up-right

whereNotBetween()arrow-up-right

whereNotIn()arrow-up-right

whereNotNull()arrow-up-right

whereNull()arrow-up-right

wrap()arrow-up-right

zip()arrow-up-right

Higher Order Messagesarrow-up-right 😉😉😉 chưa hiểu?

Laravel: Understanding Collections’ Higher Order Messages

… and the beauty of them.

Photo by Karen Vardazaryanarrow-up-right on Unsplasharrow-up-right

If may think that enough tinkering around Laravel’s Collections too much have made you some kind of Collection expert, but Higher Order Messagesarrow-up-right are one level above in understanding.

One thing I couldn’t grasp, and probably you either if you are reading this, is how they work and what problem they try to solve, but falter not, these are relatively easy to understand when you think about them as proxies to the real methods.

Get off my lawn, you dirty closure!

Higher Order Messages are basically properties that act like you were doing a very short closure. To better exemplify, let’s say we want to filter all users who are not admin.

You can see that Mike and Abby are admins because the is_admin is true, so the first thing you could do to filter them should be to call the filterarrow-up-right method of the Collectionarrow-up-right to return those who are inside another Collection instance.

While that line may work for some, we can make the filter a one line. The filter method as a Higher Order Message by the same name. It will take the value we are using to “get” as the what the closure should return for each item in the collection, being a property or an array key.

And that’s it, it’s very simple.

What about sum, for example?

Some methods do not return collections, and that’s fine. For example, the arrow-up-rightsumarrow-up-right methodarrow-up-right returns the result of a sum on the value of each item.

In this case, we can sum the users who are admins:

We can make that whole call into one line, since the sum method actually does an additive operation in PHP which is compatible with booleans, where true is 1 and false is 0. So, yes, we can sum booleans without any problem.

Can you use methods? With arguments?

Higher Order Collection are also compatible with methods, including arguments to them. The method you pass will be used for each item, so let’s make an example.

This User class has a convenient method that returns true or false if the age is above to what we’re asking.

Let’s assume we have a collection of these User class instances. The old nasty way to filter them would be using a closure to execute on each of them.

And it gets more verbose when we have to include the age from a variable outside the closure.

But that can be, again, become a one line:

And that’s all folks!

Lazy Collection trong Laravel 6.0

1. Lazy Collection trong Laravel 6.0

Bài đăng này đã không được cập nhật trong 2 năm

Laravel 6.0 đã giới thiệu Lazy Collection. Lazy collection sử dụng PHP generatorsarrow-up-right để cho phép chúng ta làm việc với một tập dữ liệu rất lớn và giữ cho mức sử dụng bộ nhớ ở mức thấp. Ví dụ với 60.000 bản ghi user. Nếu bây giờ chúng ta lấy ra tất cả các bản ghi cùng 1 lúc thì sao? Chúng ta nhận được lỗi 500 do quá tải bộ nhớ, bởi vì tất cả 60.000 user đều được tải vào bộ nhớ cùng lúc.

Và để loại bỏ việc sử dụng bộ nhớ này chúng ta sử dụng method cursor() . Method này cho phép chỉ thực hiện 1 câu truy vấn duy nhất và chỉ 1 Eloquent model được load ra tại 1 thời điểm .

Trong ví dụ này, việc gọi filter sẽ không được thực thi cho đến khi từng user được lặp lại, làm giảm đáng kể việc sử dụng bộ nhớ

Read Large File

Ngoài ra chúng ta có thể sử dụng lazy collection để đọc hàng GB khổng lồ. Method make() dùng để tạo 1 lazy collection class object

Ví dụ, chúng ta lấy 4 bản ghi với method chuck(), load các bản ghi vào LogEntry model và lặp qua các bản ghi đó.

Điểm chú ý ở đây chính là việc ta sử dụng hàm yield của php thay choreturn như Collection. Thay vì dừng thực thi hàm và trả về (return), yield trả về giá trị khi giá trị đó cần sử dụng đến mà không lưu trữ tất cả các giá trị trong bộ nhớ.

Ban có thể tìm hiểu thêm về yield tại đâyarrow-up-right

2. Tìm hiểu Lazy Collection

Last updated