Laravel Pluck() and Select() Method Differences

Created At: 2023-05-03 23:06:17 Updated At: 2023-06-03 23:41:29

Here we will cover the differences between Laravel Pluck() and Select() method. They are both part of Laravel collections. It means they are part of Laravel collection's methods. 

Pluck() method

This method retreives values based on key value pair.

 //get our categories
$result = CourseType::pluck('id', 'title',);

This would print the below results

Laravel pluck method

You see from the code we passed id and title as arguments.

The second one works as key in the code.

So in our case title is the key. That's why you see the titles in the first column of the above photo. Values are the id on the right side.

You may also pass only one argument, in that case, you will see the printed value on the right side.

//get our categories
$result = CourseType::pluck('title',);

Now see the result

Here we also see our titles are being printed. They are on the right side, meaning that it will give an id for title automitically.

These ID's are coming from database row. They are auto assigned.

Le'ts see another example

//get our categories
$result = CourseType::pluck('description','title');

Now, here you see from the code, we are passing description and title. So titles are the keys and descriptions are the values.

And values are always put on the right side.

Now, pluck() method only takes two values for arguments. If you wanna pass more, you need to use select() method.


Add Reviews