S
S
semki0962018-03-14 16:33:25
Laravel
semki096, 2018-03-14 16:33:25

How to extract data from an eloquent (laravel) model and place it in a variable as an array?

Model if I correctly understood is an object. How do I convert it to a data array of one of the fields like name. I realized that in the loop I can get the data of this field

$users = \User::where(...)->get();

        foreach ($users as $user)
        {
            echo($user->name);echo('<br>');
        }

Maybe there is a way to get an array at once, or some other way to get it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor, 2018-03-14
@semki096

User::where(...)->pluck("name");

A
Andreo, 2018-03-14
@chupacabramiamor

1. It's not just an object. And what class object should you find in the dock .
2. Having found what get() returns, you can easily, like a snap of the fingers of one hand, convert the entire array, and the second hand into an array with the desired structure.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question