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

Asked

Viewed 241 times

2

I’m pulling the data from a table to a select, but I have fields where the information is equal, for example:

Controller:

$amostragens = Amostragem::all();

View:

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

Table:

inserir a descrição da imagem aqui

Then it ends up getting repeated thing inside the select when I will display the "analyte".

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

1 answer

2


Using GroupBy()

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

Using Distinct()

Fluent

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

Eloquent

Amostragem::distinct()->get(['analito']);
  • 1

    It worked @Zoom, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.