How to avoid repeated data in a select using Laravel?

Oscar :

I have this select

enter image description here

I'm getting all of this information from a table in my database... so I have some repeated rows... I would like to found a kind of if or something to avoid the repeated information in the select... this is my select

<div class="form-group row col-xs-3 col-md-4" >
<label class="control-label col-md-4" >Cartera: </label>
<div class="col-md-8">
 <select name="carteras_id" id="carteras_id" class="form-control">
@foreach ($carteras as $cartera)
  <option value="{{ $cartera->carteras_id }}" class="form-control">{{ $cartera->cartera }}</option>
@endforeach
</select>
</div>

This is my query

$carteras = DB::table('tbl_perimetros')
            ->join('tbl_lista_carteras', 'tbl_perimetros.carteras_id', '=', 'tbl_lista_carteras.id')
            ->get();
TsaiKoga :

It seems tbl_lista_carteras has many tbl_perimetros, so when you use join to these two table, the tbl_lista_carteras datas will be duplicataed, and you only need to display the tbl_lista_carteras's datas, you can get them from tbl_lista_carteras directly:

$carteras = DB::table('tbl_lista_carteras')
    ->select('id','cartera')
    ->get();

And display them in your view:

@foreach ($carteras as $cartera)
  <option value="{{ $cartera->id }}" class="form-control">{{ $cartera->cartera }}</option>
@endforeach

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=14018&siteId=1