Well, the difference is this: When you invoke lists
you cause only the specific fields to be specified in the Orm
and bring them in array
.
Example:
Usuario::lists('nome', 'id');
Is the same as :
SELECT nome, id FROM usuarios
The case of the method all
followed by lists
something else happens.
When we call Usuario::all()
we bring all the results in the table usuarios
and with all fields selected.
SELECT * FROM usuários
The method all
return an object called Illuminate\Database\Eloquent\Collection
. This object, in turn, has the method all
. But internally, it uses the so-called function array_pluck
to capture only the two past items, such as key and value.
Performance
The difference in performance is remarkable between the two explanations. lists
won both in the execution of the query and in the amount of data brought to PHP.
If you are just going to bring a list containing the key-value pair of the bank, you should use lists
.
But there are cases where, in addition to bringing the user’s complete data, for reasons of need of a certain type, you would want to transform an object Eloquent\Collection
in a array
.
In this case, we are talking about a concept of object orientation called Reuse.
An example of this is to need to bring all user data to make a listing in a table, but at the same time need a listing of these users in a select (then it will be necessary to use lists
in that collection, not to make another SELECT in the bank).
Let me give you an example:
$usuario = Usuario::all();
// Preciso da lista de nomes e ids para passar para um "select" no html
$usuarios_lista = $usuarios->lists('nome', 'id');