I try to walk a variable using the helpers of Laravel but it trhows an error, and when I do the same with html tags it works correctly.
{{ Form::select('categoria', array(
@foreach($enlaceid as $categoria)
$categoria->id => $categoria->nombre {{-- I tryed too with englobing with {{ }}
)) }}
@endforeach
<select name="categoria">
@foreach($enlaceid as $categoria)
<option value= " {{ $categoria->id }} "> {{$categoria->nombre}} </option>
@endforeach
</select>
Use the lists()
method and pass that to your Form::select()
method. Like so:
$categories = Category::lists('name', 'id');
In your view:
{{ Form::select('category_id', $categories) }}
for you want to use foreach laravelcollective in laravel 5.2 or above, you can use same as @garethDaine but above laravel 5.2, you using pluck()
instead lists()
because lists()
function was depracated in laravel 5.2.
so in the examples is:
$banks = Bank::pluck('name', 'id');
instead
$banks = Bank::lists('name', 'id');
then in your view form:
{!! Form::select('bank_id', $banks, null, ['class' => 'form-control']) !!}
Note: 1st array in you pluck() will be name display on you <option>
and second will be value="1"
as example.
The answer by Gareth Daine edited by Drew Hammond worked for me, with a slight edit
My Controller
$category = Category::all();
My View
{!! Form::select('category_id', $categories->pluck('name', 'id')) !!}
In case you want to add a default value also to your options you need to do this:
$categories = Category::lists('name', 'id')->toArray();
In your view:
{{ Form::select('category_id', array('0' => 'Default') + $categories) }}