10 less-known (but awesome!) Laravel Collections methods

Laravel Eloquent is awesome – probably I don’t need to tell you that. What is less known is the list of methods to work with Eloquent Collections. You can filter them, slice them, easily modify etc. But let’s look at it one by one.

A little notice here – that Collections are a broader term, whereas Eloquent Collections are the implementation of these broader Collections. So everything that we cover here in this article can be used for both instances.

So, for example, you have Laravel code like this:

In this case $books is a collection, which is basically an array with additional features. For example it can look like this:

So what can we do with it? Important note: all those functions below are called without querying the database – so you run a query once and then perform all actions with collection “offline”.

1. avg()

If you want to calculate average price of the book, you don’t need to query database separately for that.


2. chunk()

If you want to split the results by equal parts, for example to view them as columns, it can look like this:

And the result:


3. contains()

Simple – check whether our collection contains a certain value in one of the fields:


4. every()

Another method of splitting the collection into a different columns. This time – every() would form a new collection, consisting of every N-th element.

Result:

Have you tried our tool to generate Laravel adminpanel without a line of code?
Go to QuickAdminPanel.com

5. filter()

This method was already a topic of separate mini-article so will just briefly repeat:


6. forget()

This is a method to use if you want to get rid of one of the columns.

Result:


7. implode()

Really similar to a well-known PHP array implode() function. The result is a joined string:

Result:


8. keyBy()

This is a really useful one to use with foreach loops later. Basically, it transforms a collection into an array with your chosen key.

Result:


9. map()

If you want to extract some values and perform some actions with them – there’s not only foreach().

Result:


10. pluck()

This method allows you to extract just one column easily.

Result:


So this is hand-picked 10 less-known methods, but it’s not even a half of what’s available. You can find full list with short examples in the official documentation for Eloquent Collections and Basic Collections.

Keep digging deeper into Laravel world!

Want more tips and articles like this?
20-50 useful links every week on our weekly newsletter - comes out every Thursday!



13 thoughts on “10 less-known (but awesome!) Laravel Collections methods

  1. My favourite method at the moment is groupBy(‘key’). Super useful for grouping a list of events by date.

  2. Thanks for this. Just wanted to add that you can use pluck() to extract two columns which is super useful for creating select boxes.

    $book_list = $books->pluck(‘title’, ‘id’);

    $book_list is now a collection that looks like:
    [
    1 => ”Lean Startup’],
    2 => [‘The One Thing’],
    3 => [‘Laravel: Code Bright’],
    4 => [‘The 4-Hour Work Week’],
    ]

    Which you can then use in a blade template via the Illuminate\Html\FormBuilder select() method like:

    Form::select(‘book_id’, $book_list, old(‘book_id’, $default_book_id), [‘class’ => ‘form-control’]);

Leave a Reply

Your email address will not be published. Required fields are marked *