How to make a "select distinct" in a model in Laravel?

1

I'm pulling data from a table to a select , however I have fields where information is the same, for example:

Controller:

$amostragens = Amostragem::all();

View:

@foreach($amostragens as $amostragem)
     <option value="{{ $amostragem->id }}">{{ $amostragem->analito }}</option>
@endforeach

Table:

SoitendsupbeingrepeatedwithinselectwhenIdisplaythe"analyte".

I need to show for example only 1 "Ethyl Acetate", and in another select or I already have something prepared to display all the "collector" options that have the selected analyte.

    
asked by anonymous 02.05.2016 / 21:22

1 answer

1

Using GroupBy()

$amostragens = Amostragem::select('id', 'analito')->groupBy('analito')->get();

Using Distinct()

Fluent

DB::table('amostragem')->distinct()->get(['analito']);

Eloquent

Amostragem::distinct()->get(['analito']);
    
02.05.2016 / 21:28