• Czas czytania ~9 min
  • 08.04.2023

Laravel Eloquent używa kolekcji, aby zwrócić wyniki. Kolekcje zawierają bardzo przydatne metody, które czynią je bardzo potężnymi i pomocnymi w użyciu. Możesz je filtrować, modyfikować i wiele więcej z nimi bardzo wygodnie. W tym samouczku przyjrzymy się metodom zbierania laravel.

Kolekcje nie ograniczają się tylko do elokwentnych wyników, ale mogą być używane osobno. Elokwentne wyniki wykorzystują kolekcje. Możesz po prostu utworzyć kolekcję, przekazując tablicę do collect funkcji pomocniczej. Wszystkie kolejne metody zbierania wymienione poniżej mają zastosowanie zarówno do zbiorów elokwentnych, jak i do samych zbiorów.

Powiedzmy, że masz model posta. Znajdziesz wszystkie posty z php kategorią.

$posts = App\Post::where('category', 'php')->get();

The above command returns a collection. A collection is a laravel class that uses arrays internally and adds many features to them.

Kolekcję można utworzyć po prostu za pomocą collect takiej metody.

$collection = collect([
    [
        'user_id' => '1',
        'title' => 'Helpers in Laravel',
        'content' => 'Create custom helpers in Laravel',
        'category' => 'php'
    ],
    [
        'user_id' => '2',
        'title' => 'Testing in Laravel',
        'content' => 'Testing File Uploads in Laravel',
        'category' => 'php'
    ],
    [
        'user_id' => '3',
        'title' => 'Telegram Bot',
        'content' => 'Crypto Telegram Bot in Laravel',
        'category' => 'php'
    ],
]);

Above array is actually the values of our Post model. In this tutorial, we will be using this array for simplification. Remember everything will work the same way for the eloquent as well.

W ten sposób możesz po prostu tworzyć kolekcje. Możesz zastosować całą metodę pomocnika kolekcji dostępną w Laravel.

Gdy stosujemy metody pomocnicze do elokwentnych kolekcji, nie wysyłają one zapytań do bazy danych. Wszystkie wyniki, które chcemy uzyskać z bazy danych, są najpierw pobierane, a następnie używamy metod zbierania do ich filtrowania i modyfikowania bez żadnych zapytań do bazy danych.

filter()

filter, jedna z najbardziej użytecznych metod zbierania laravel, umożliwia filtrowanie kolekcji za pomocą wywołania zwrotnego. Przekazuje tylko te elementy, które zwracają wartość true. Wszystkie pozostałe elementy zostaną usunięte. filter Zwraca nową instancję bez zmieniania oryginalnej instancji. Akceptuje value i key jako dwa parametry w wywołaniu zwrotnym.

$filter = $collection->filter(function($value, $key) {
    if ($value['user_id'] == 2) {
        return true;
    }
});

$filter->all();

all method returns the underlying array. Above code returns the following response.

[
    1 => [
        "user_id" => 2,
        "title" => "Testing in Laravel",
        "content" => "Testing File Uploads in Laravel",
        "category" => "php"
    ]
]

search()

search method is used to search the collection for a given value. If the value is present in the collection, the key of the value is returned. If the value does not matches any item, false is returned.

$names = collect(['Alex', 'John', 'Jason', 'Martyn', 'Hanlin']);

$names->search('Jason');

// 2

By default, the search is done using loose comparison. You can pass true as the second argument to the search method to use strict comparison.

Możesz również przekazać własną funkcję wywołania zwrotnego search do metody. Spowoduje to zwrócenie klucza pierwszego elementu, który przejdzie test prawdy wywołania zwrotnego.

$names = collect(['Alex', 'John', 'Jason', 'Martyn', 'Hanlin']);

$names->search(function($value, $key) {
    return strlen($value) == 6;
});

// 3

chunk()

chunk method is used to split the collection into multiple smaller collections of given size. It is useful displaying collections into the grid.

$prices = collect([18, 23, 65, 36, 97, 43, 81]);

$prices = $prices->chunk(3);

$prices->toArray();

Response from the above code.

[
    0 => [
        0 => 18,
        1 => 23,
        2 => 65
    ],
    1 => [
        3 => 36,
        4 => 97,
        5 => 43
    ],
    2 => [
        6 => 81
    ]
]

dump()

dump method dumps the collection’s items. It is useful for debugging and finding what’s inside the collection at any point in the collection pipeline.

$collection->whereIn('user_id', [1, 2])
    ->dump()
    ->where('user_id', 1);

dump response from the above code.

laravel collection methods dump

map()

map method is used to iterate through the full collection. It accepts a callback as an argument. value and the key is passed to the callback. Callback can modify the values and return them. Finally, a new collection instance of modified items is returned.

$changed = $collection->map(function ($value, $key) {
    $value['user_id'] += 1;
    return $value;
});

return $changed->all();

Basically, it incremented user_id by one.

Odpowiedź z powyższego kodu wygląda tak.

[
    [
        "user_id" => 2,
        "title" => "Helpers in Laravel",
        "content" => "Create custom helpers in Laravel",
        "category" => "php"
    ],
    [
        "user_id" => 3,
        "title" => "Testing in Laravel",
        "content" => "Testing File Uploads in Laravel",
        "category" => "php"
    ],
    [
        "user_id" => 4,
        "title" => "Telegram Bot",
        "content" => "Crypto Telegram Bot in Laravel",
        "category" => "php"
    ]
];

zip()

Metoda zip dołącza wartości danej tablicy do wartości kolekcji. Wartości są dodawane do tego samego indeksu, co oznacza, że pierwsza wartość tablicy zostanie scalona z pierwszą wartością kolekcji. Tutaj używam tablicy kolekcji, którą stworzyliśmy powyżej. Działa to w ten sam sposób również z elokwentnymi kolekcjami.

$zipped = $collection->zip([1, 2, 3]);

$zipped->all();

JSON Response looks like this.

laravel collection methods zip

To w zasadzie tyle. Jeśli liczba tablicy jest mniejsza niż liczba kolekcji, laravel doda null na końcu pozostałych elementów kolekcji. Ponadto, jeśli liczba tablicy jest większa niż liczba kolekcji, laravel doda a dla elementów kolekcji, a null następnie kolejną wartość tablicy.

whereNotIn()

Możesz użyć whereNotIn metody, aby po prostu filtrować kolekcję według wartości klucza niezawartej w danej tablicy. Jest to w zasadzie przeciwieństwo whereIn. Ponadto ta metoda używa luźnego porównania == podczas dopasowywania wartości.

Przefiltrujmy gdzie nie ma ani 1 2.

$collection->whereNotIn('user_id', [1, 2]);

The above statement will return only the last item from the $collection created above. The first argument is the key and the second argument is an array of values. In case of eloquent, the first argument will be the name of the column and the second argument will be an array of values.

user_id $collection max()pluck()

max method returns the maximum value of a given key. You can find the maximum user_id by calling max. It’s normally used for things like price or any other number but for the sake of demonstration let’s use user_id. It can also be used with strings and in that case, Z > a.

$collection->max('user_id');

Above statement will return the maximum user_id which in our case is 3.

pluck method returns all of the values for a key. It is useful for extracting values of one column.

$title = $collection->pluck('title');
$title->all();
The result looks like this.
[
  "Helpers in Laravel",
  "Testing in Laravel",
  "Telegram Bot"
]

When working with eloquent, you can pass a column name as an argument to extract its values. pluck also accepts a second argument and in the case of eloquent collections, it can be another column name. It will result in collection keyed by the values of the second argument.

$title = $collection->pluck('user_id', 'title');
$title->all();

The result looks like this:

[
    "Helpers in Laravel" => 1,
    "Testing in Laravel" => 2,
    "Telegram Bot" => 3
]

each()

each is a simple method for iterating over the full collection. It accepts a callback with two arguments: item it is iterating through and the key. Key is 0 based index.

$collection->each(function ($item, $key) {
    info($item['user_id']);
});

Above, it is simply logging the user_id of each item.

Podczas rejestrowania kolekcji elokwentnych można uzyskać dostęp do wszystkich wartości kolumn jako właściwości elementu. Oto jak możemy iterować wszystkie posty.

$posts = App\Post::all();

$posts->each(function ($item, $key) {
    // Do something
});
If you return false from your callback, it will stop iterating over items.
$collection->each(function ($item, $key) {
    // Tasks
    if ($key == 1) {
        return false;
    }
});

tap()

tap() method allows you to tap into the collection at any point. It accepts a callback and passes and passes the collection to it. You can do anything with the items without changing the collection itself. So, you can use tap to peak into the collection at any point without mutating the collection.

$collection->whereNotIn('user_id', 3)
    ->tap(function ($collection) {
        $collection = $collection->where('user_id', 1);
        info($collection->values());
    })
    ->all();

In the tap method used above, we modified the collection and then logged the value. You can do anything you want with the collection inside the tap. Response from the above command is:

[
    [
        "user_id" => "1",
        "title" => "Helpers in Laravel",
        "content" => "Create custom helpers in Laravel",
        "category" => "php"
    ],
    [
        "user_id" => "2",
        "title" => "Testing in Laravel",
        "content" => "Testing File Uploads in Laravel",
        "category" => "php"
    ]
]

You can see that tap does not modify the collection instance. Learn more about tap helper function and tap collection method in detail.

pipe()forget()avg()

pipe method is very similar to the tap method in a sense that both of them are used inside the collection pipeline. pipe method passes the collection to the callback and return the result.

$collection->pipe(function($collection) {
    return $collection->min('user_id');
});

Response from the above command is 1. If you return an instance of collection from the pipe callback, you can chain further methods as well.

contains method simply checks if the collection contains a given value. It is only true when you pass a single argument.

$contains = collect(['country' => 'USA', 'state' => 'NY']);

$contains->contains('USA');
// true

$contains->contains('UK');
// false

If you pass a key / value pair to the contains method, it will check whether the given pair exists or not.

$collection->contains('user_id', '1');
// true

$collection->contains('title', 'Not Found Title');
// false

You can also pass a callback as an argument to the callback method. Callback will run for every item in the collection and if any of them passes the truth test, it will return true else false.

$collection->contains(function ($value, $key) {
    return strlen($value['title']) < 13;
});
// true

Callback accepts two arguments value of the currently iterating item and the key. Here we are simply checking if the length of the title is less than 13. In Telegram Bot it is 12, so it returned true.

forget simply removes the item from the collection. You simply pass a key and it will remove that item from the collection.

$forget = collect(['country' => 'usa', 'state' => 'ny']);

$forget->forget('country')->all();

Response from the above code:

[
    "state" => "ny"
]

forget does not work on multi-dimensional arrays.

avg method returns the average value. You simply pass a key as an argument and the avg method returns the average. You can also use the average method which is basically an alias for avg.

$avg = collect([
    ['shoes' => 10],
    ['shoes' => 35],
    ['shoes' => 7],
    ['shoes' => 68],
])->avg('shoes');

Above code returns 30 which is the average of all four numbers. If you do not pass any key to avg method and all the items are numbers, it will return the avg of all numbers. If the key is not passed as an argument and the collection contains key / value pairs, avg method returns 0.

$avg = collect([12, 32, 54, 92, 37]);

$avg->avg();

The above code returns 45.4 which is the average of all five numbers.

Tych metod zbierania laravel można używać do pracy z kolekcją we własnych projektach.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

O

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

O autorze CrazyBoy49z
WORK EXPERIENCE
Kontakt
Ukraine, Lutsk
+380979856297