--- title: Examples of using Laravel Collections in Drupal tags: - drupal - drupal-7 - drupal-8 - drupal-planet - laravel - laravel-collections - php has_tweets: true --- {% block excerpt %} Since starting to work with Laravel as well as Drupal and Symfony, watching Adam Wathan’s [Refactoring to Collections][0] course as well as [lessons on Laracasts][6], I’ve become a fan of [Laravel’s Illuminate Collections][1] and the object-orientated pipeline approach for interacting with PHP arrays. In fact I’ve given a talk on [using Collections outside Laravel][2] and have written a [Collection class module][3] for Drupal 7. I’ve also tweeted several examples of code that I’ve written within Drupal that use Collections, and I thought it would be good to collate them all here for reference. {% endblock %} {% block content %} Thanks again to [Tighten][4] for releasing and maintaining the [tightenco/collect library][5] that makes it possible to pull in Collections via Composer.
Putting @laravelphp's Collection class to good use, cleaning up some of my @drupal 8 code. Thanks @TightenCo for the Collect library! pic.twitter.com/Bn1UfudGvp
— Oliver Davies (@opdavies) August 18, 2017', } %} {% include 'tweet' with { class: 'block mb-4 lg:w-1/2 lg:px-2 lg:mb-0', data_cards: true, content: 'Putting more @laravelphp Collections to work in my @drupal code today. 😁 pic.twitter.com/H8xDTT063X
— Oliver Davies (@opdavies) February 14, 2018', } %} {% include 'tweet' with { class: 'block mb-4 lg:w-1/2 lg:px-2 lg:mb-0', data_cards: true, content: 'I knew that you could specify a property like 'price' in Twig and it would also look for methods like 'getPrice()', but I didn't know (or had maybe forgotten) that @laravelphp Collections does it too.
This means that these two Collections return the same result.
Nice! 😎 pic.twitter.com/2g2IfThzdy
More @laravelphp Collection goodness, within my #Drupal8 project! pic.twitter.com/mWgpNbNIrh
— Oliver Davies (@opdavies) August 10, 2018', } %} {% include 'tweet' with { class: 'block mb-4 lg:w-1/2 lg:px-2 lg:mb-0', data_cards: true, content: 'Some more #Drupal 8 fun with Laravel Collections. Loading the tags for a post and generating a formatted string of tweetable hashtags. pic.twitter.com/GbyiRPzIRo
— Oliver Davies (@opdavies) August 23, 2018', } %}