user5818995
user5818995

Reputation:

Difference between pluck() and lists() in laravel?

This is so confusing to me. I don't see any difference between these two methods. If I var_dump() the object returned by these methods, they are exactly the same but the book by Dayle Rees says that pluck() returns a single value from the given column (the first one) while the lists() method returns all the values from the given column. I can't even figure out why two different methods exist to do the same thing.

Example

Route::get('getalbum', function() {
    $data = \App\Album::pluck('artist');
    var_dump($data); // a lot of text, let's call it 'object'

    $data = \App\Album::lists('artist');
    var_dump($data); // exact , exact, exact same 'object'
});

Upvotes: 8

Views: 9186

Answers (1)

Can Vural
Can Vural

Reputation: 2077

From the docs, Deprecations section

The following features are deprecated in 5.2 and will be removed in the 5.3 release in June 2016

The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature remains the same.

So yes they are same. It's just there for backward compatibility.

Source code

Upvotes: 12

Related Questions