Introduction
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 pluck
.
Requisite
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 pluck
and map
method and which one better to use.
You must know what's the difference between Eloquent and Support collections first.
Eloquent Collection
All Eloquent methods that return more than one model result will return instances of the
Illuminate\Database\Eloquent\Collection
class, 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
Support Collection
The
Illuminate\Support\Collection
class 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 pluck
or get
methods like this:
Using get() method
1 2/**3 * @var $posts4 * @return Illuminate\Database\Eloquent\Collection5**/6 7$posts Post::get('title', 'description');8 9return $posts;
Using pluck() method:
1 2/** 3 * @var $posts 4 * @return Illuminate\Support\Collection 5**/ 6 7$posts Post::pluck('title', 'description'); 8 9// ['title' => 'description'] returns a key => value10 11return $posts;
As you can see when we are using get to fetch specific columns. Laravel returns Illuminate\Database\Eloquent\Collection
.
And hen use pluck
method. Laravel returns Illuminate\Support\Collection
.
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.
1 2/**3 * @var $posts4 * @return Illuminate\Support\Collection5**/6 7$posts = Post::all()->map->only('title', 'description', 'excerpt', 'slug');8 9// ['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.
Use Cases
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
whereIn
orget
orwhere
... and all this great database method. Use the standardget
and 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
pluck
ormap
with only methods.
Conclusion
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 map->only(...)
method.
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.