Laravel 8 Eloquent pluck() Example

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/laravel-8-eloquent-pluck-example

Laravel 8 Eloquent pluck() method helps us to extract certain values into 1 dimension array. It s…


This content originally appeared on DEV Community and was authored by codeanddeploy

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/laravel-8-eloquent-pluck-example

Laravel 8 Eloquent pluck() method helps us to extract certain values into 1 dimension array. It shortens our code if we want only to get the specific field values into 1 dimension array so that we don't need to loop the result collection to get certain values using this method. In this post, we are using a posts table with title, description, and body fields.

In this example, we want to display the title only into 1 dimension array. Or should be like this:

Array
(
    [0] => Post 3
    [1] => Post 1
    [2] => Post 2
    [3] => Post 3
    [4] => Post 4
)

But to do that in we need to use all() method in eloquent as you can see in the following below:

$posts = Post::all();

$postsTitles = [];
foreach($posts as $post) {
    $postsTitles[] = $post->title;
}

print_r($postsTitles);die;

As you can see above we loop the post result then we store the post title to our array variable which is not good because our code is long.

And the result is the same:

Array
(
    [0] => Post 3
    [1] => Post 1
    [2] => Post 2
    [3] => Post 3
    [4] => Post 4
)

But using the pluck() method in Laravel our code will be short. See the below example:

$posts = Post::all()->pluck('title')->toArray();

print_r($posts);die;

And the result is the same above:

Array
(
    [0] => Post 3
    [1] => Post 1
    [2] => Post 2
    [3] => Post 3
    [4] => Post 4
)

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-eloquent-pluck-example if you want to download this code.

Happy coding :)


This content originally appeared on DEV Community and was authored by codeanddeploy


Print Share Comment Cite Upload Translate Updates
APA

codeanddeploy | Sciencx (2022-03-29T07:51:58+00:00) Laravel 8 Eloquent pluck() Example. Retrieved from https://www.scien.cx/2022/03/29/laravel-8-eloquent-pluck-example/

MLA
" » Laravel 8 Eloquent pluck() Example." codeanddeploy | Sciencx - Tuesday March 29, 2022, https://www.scien.cx/2022/03/29/laravel-8-eloquent-pluck-example/
HARVARD
codeanddeploy | Sciencx Tuesday March 29, 2022 » Laravel 8 Eloquent pluck() Example., viewed ,<https://www.scien.cx/2022/03/29/laravel-8-eloquent-pluck-example/>
VANCOUVER
codeanddeploy | Sciencx - » Laravel 8 Eloquent pluck() Example. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/29/laravel-8-eloquent-pluck-example/
CHICAGO
" » Laravel 8 Eloquent pluck() Example." codeanddeploy | Sciencx - Accessed . https://www.scien.cx/2022/03/29/laravel-8-eloquent-pluck-example/
IEEE
" » Laravel 8 Eloquent pluck() Example." codeanddeploy | Sciencx [Online]. Available: https://www.scien.cx/2022/03/29/laravel-8-eloquent-pluck-example/. [Accessed: ]
rf:citation
» Laravel 8 Eloquent pluck() Example | codeanddeploy | Sciencx | https://www.scien.cx/2022/03/29/laravel-8-eloquent-pluck-example/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.