Laravel Pluck Method Example

Laravel pluck() method example; Through this tutorial, i am going to show you how to use pluck method in laravel apps.

The laravel eloquent pluck() method is used to extract values from array or collection.

Now, i will take some examples for extract data from collection in laravel apps; is as follows:

Example 1 – Laravel Extract Single Value From Collection Using Pluck()

Let’s take first example using laravel pluck() method to extract single value from collection; is as follows:

   $name = User::pluck('name');
  
   dd($name);

Example 2 – Laravel Extract Multiple Value From Collection Using Pluck()

Let’s take first example using laravel pluck() method to extract multiple value from collection; is as follows:

   $data = User::pluck('name', 'id');
  
   dd($data);

Example 3 – Laravel Extract Values From Collection Using Pluck() with Relationship

Let’s take first example using laravel pluck() method to extract values from collection with relationship; is as follows:

   $users = User::with('profile')->get();
   $bio = $users->pluck('profile.bio'); // Get all bio of all users profile
  
   dd($bio);

Recommended Laravel Tutorials

Leave a Comment