Use App\Categoria::orderBy('nome')->get()
, although this is not a good recommendation. You should see the data ready for View
of Controller
, but that's how it should be.
<div class="form-group">
<label for="nome">Categoria do Produto</label>
<select id="categoria_id" name="categoria_id" class="form-control">
<option value="">Selecione</option>
@foreach (App\Categoria::orderBy('nome')->get() as $categoria)
<option value="{{ $categoria->id }}">{{ $categoria->nome }}</option>
@endforeach
</select>
</div>
The ideal practice would be to send only the ready-made data to your View
:
Controller
public function exibe()
{
$data['categories'] = \App\Categoria::orderBy('nome')->get();
return view('view1', $data);
}
View
<div class="form-group">
<label for="nome">Categoria do Produto</label>
<select id="categoria_id" name="categoria_id" class="form-control">
<option value="">Selecione</option>
@foreach ($categories as $categoria)
<option value="{{ $categoria->id }}">{{ $categoria->nome }}</option>
@endforeach
</select>
</div>
This is an example, dummy data
References: