Translation sql to eloquent Laravel

2

Does anyone know how I can translate this query to eloquen?

select distinct usuario_ad from usuarios where asignacion='null';
    
asked by zereft 24.10.2018 в 04:04
source

2 answers

1

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

    
answered by 24.10.2018 / 04:13
source
2

Finally find the solution, thanks shadow.

$usuariosOpciones =usuarios::
 where('asignacion', 'null')
 ->pluck('usuario_ad', 'user_id')
 ->unique(); 
    
answered by 25.10.2018 в 02:11