Laravel Array

The pluck helper method is used to retrieve a list of specific values from a given $array. It is most useful when used against arrays of objects, but will also work with arrays just as well.

Signature

The signature of the pluck method is:

While resources transform a single model into an array, resource collections transform a collection of models into an array. However, it is not absolutely necessary to define a resource collection class for each one of your models since all resources provide a collection method to generate an 'ad-hoc' resource collection on the fly. Today, laravel check if array is empty in blade is our main topic. We will use laravel check if array is empty in controller. You can understand a concept of laravel not empty check. I explained simply step by step check empty array in laravel blade.

Laravel Please sign in or create an account to participate in this conversation. The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!

Example Use

Let's use the following class to describe a person:

We can create an array of people like so:

At this point all we have is a simple Person class definition an array of person objects, called $people. However, it would be really useful if we had an array of everyone's last name. This is a perfect use case for the pluck method:

Array

The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything! Laravel documentation: Using Array Syntax.

The value of $lastNames would then look like this:

The pluck function will also work on array of arrays. Consider an array of the same people, represented instead by arrays:

We could again get an array of all the last names, just like before:

And just as before, the value of $lastNames would be:

Returning an array with a key/value pair

We can also instruct the pluck method to return an array that contains both a key and a value. We do this by passing in a third argument to the pluck function: the $key.

Laravel Array Map

Let's reuse the people array:

We can get an array with everyone's firstName as the key, and their lastName as the value like so:

The value of $peopleList would now look like this:

This can be reversed, so that the lastName becomes the key and the firstName becomes the value:

The value of $peopleList would now look like this:

Working With Nested Arrays or Objects

The $key supports dot notation, which implies that we can search for keys at arbitrary depths when constructing the final array. Let's add a new Job class, which will just be a very simple representation of a job:

Now we will modify our Person class to accept a Job as one of its parameters:

After all of this work, let's build a new array of people, this time with jobs:

Laravel Array

After all of this work, we now have an array that looks like this:

What we want to do now is return an array where the job name is the key, and the person's first name is the value. Let's call this array $filledPositions:

Notice that this time the value for $key is expressed in dot notation. This will produce the following results:

Laravel Array Functions

The same techniques will also work on nested arrays.

Global array_pluck Helper Function

The array_pluck function is a shortcut to calling Arr::pluck. This function is declared in the global namespace.

Laravel Array Unique