Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jasonlbeggs/cddb01e1b33149c614808e4fa6263b48 to your computer and use it in GitHub Desktop.
Save jasonlbeggs/cddb01e1b33149c614808e4fa6263b48 to your computer and use it in GitHub Desktop.

pluck() Collection Method $key Parameter

Recently, I learned (or re-learned) that the pluck() method on a Laravel Collection accepts a second parameter to specify how you'd like the Collection keyed. Super handy in certain cases!

<?php

$posts = collect([
    ['slug' => 'post-1', 'title' => 'Taylor is cool'],
    ['slug' => 'post-2', 'title' => 'Tyler is the real MVP'],
]);

$plucked = $posts->pluck('title', 'slug');

// ['post-1' => 'Taylor is cool', 'post-2' => 'Tyler is the real MVP']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment