Does anyone know how I can translate this query to eloquen?
select distinct usuario_ad from usuarios where asignacion='null';
Does anyone know how I can translate this query to eloquen?
select distinct usuario_ad from usuarios where asignacion='null';
You can do it in the following way
WITH THE QUERY BUILDER FLUENT
$data = \DB::table('usuarios')
->distinct('usuario_ad')
->where('asignacion', 'null')
->get();
To the method distinct()
you pass in quotes the name of the column for which you want to find different values
The where
method is passed to the end and the use of the = sign is optional
At the end you use get
to get the results
WITH THE ELOQUENT ORM
Or if you have a model called Usuario
you can do it this way entirely with Eloquent
$data = Usuario::distinct('usuario_ad')
->select('usuario_ad')
->where('asignacion', 'null')
->get();
UPDATE
To select specific columns of the table, then after the method distinct()
invoke the method select()
and pass the name of the columns you want; in this case usuario_ad
Finally find the solution, thanks shadow.
$usuariosOpciones =usuarios::
where('asignacion', 'null')
->pluck('usuario_ad', 'user_id')
->unique();