Sometimes when we are working with collection we need just to pluck a specific amount of values/columns from an array or from the database. Laravel ships with standard chaining method used with collections called
pluck but the issue here you can just fetch 2 values/columns no more, no less.
In this quick example I will show you how to pluck multiple columns from a collection using
map instead of
Basic Understanding on laravel framework.
Difference between Eloquent Collection & Support Collection
To become an artisan on those things you must know what a query returns as an instance. So before we dive deep in
map method and which one better to use.
You must know what's the difference between Eloquent and Support collections first.
All Eloquent methods that return more than one model result will return instances of the
Illuminate\Database\Eloquent\Collectionclass, including results retrieved via the get method or accessed via a relationship. The Eloquent collection object extends Laravel's base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models. Be sure to review the Laravel collection documentation to learn all about these helpful methods!
For more info take a look here
Illuminate\Support\Collectionclass provides a fluent, convenient wrapper for working with arrays of data. For example, check out the following code. We'll use the collect helper to create a new collection instance from the array, run the strtoupper function on each element, and then remove all empty elements:
For more information take a look also here
Pluck Attributes Using Pluck Method
Imagine we had posts, and we want to grub just the title and the description. So, we can do it with
get methods like this:
Using get() method
12/**3 * @var $posts4 * @return Illuminate\Database\Eloquent\Collection5**/67$posts Post::get('title', 'description');89return $posts;
Using pluck() method:
12/**3 * @var $posts4 * @return Illuminate\Support\Collection5**/67$posts Post::pluck('title', 'description');89// ['title' => 'description'] returns a key => value1011return $posts;
As you can see when we are using get to fetch specific columns. Laravel returns
And hen use
pluck method. Laravel returns
Pluck Attributes Using Map Method
In case you want more than two columns from the database, and you don't know how. Here is the way on how to do it.
12/**3 * @var $posts4 * @return Illuminate\Support\Collection5**/67$posts = Post::all()->map->only('title', 'description', 'excerpt', 'slug');89// ['title', 'description', 'excerpt', 'slug'] ...
As you can see here. We map through all the posts and pick only the columns we want.
So, With this trick you can catch columns as much as you can.
You may be wondering. When I use map method and when I use get method? And I'm telling you are in the right path if you think this way.
So, as we saw There is a two type of collection. Support Collection and Database Collection.
- If you want to chain methods like
where... and all this great database method. Use the standard
getand pick your columns to make the queries run faster.
- If you want to just grub the result to show them on the frontend or do something with them. Use
mapwith only methods.
We learned today something very interesting on laravel world.
So, the article telling you above. If you want to grub just one column from the collection use
pluck method and if you want to get multiple columns use
I hope you learned something new from this article. And if you like my content don't forget to follow me on Twitter or to connect with me on LinkedIn to learn more useful stuff.