0

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>
abdom
  • 1
  • 1
  • 1
  • 1

4 Answers4

5

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) }}
Drew Hammond
  • 588
  • 5
  • 19
Gareth Daine
  • 4,016
  • 5
  • 40
  • 67
3

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.

1

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')) !!}
Seun Oyebode
  • 41
  • 1
  • 6
0

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) }}
paulalexandru
  • 9,218
  • 7
  • 66
  • 94