0

I'm new to Laravel and Blade and have been trying to create a view using Illuminate/Html.

I have a table called service_locations(location_id, location_area).

Using the above table I'm trying to populate the below dropdown list:

<div class="form-group">
{!! Form::label('location', 'Location:') !!}
{!! Form::select('location', array(

    @foreach($locations as $local)
       '{{ $local->location_id }}' => '{{ $local->location_area }}', 
    @endforeach

), null, ['class' => 'form-control']) !!}
</div>

But as I attempt to do so, I am getting the following error in the second-last line (), null, ['class' => 'form-control']) !!}):

syntax error, unexpected '<', expecting ')'

I am not able to figure out the issue with the above code.

Edit 1 Here's what my controller looks like:

<?php namespace App\Http\Controllers;

use App\service_location;
use App\service_type;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PagesController extends Controller {

    public function index()
    {
        $locations = service_location::all();
        $services = service_type::all();

        return view('home.index', compact('locations','services'));
    }
}
ikartik90
  • 2,695
  • 6
  • 26
  • 38
  • You can't use blade structures (`@foreach`) to build a PHP array. You should definitely prepare that data in the controller and pass it to the view – lukasgeiter Apr 01 '15 at 08:54
  • @lukasgeiter Please recheck. I've updated the question to include my controller code here as well. – ikartik90 Apr 01 '15 at 09:12

2 Answers2

1

you cant use blade that way,

but you can achieve the same result with

{!! Form::select('location', $locations->lists('id','location_area'), null, ['class' => 'form-control']); !!}
terry low
  • 10,218
  • 1
  • 14
  • 12
  • I have already fetched all the necessary records in the table into the `$locations` object and passed them using the `compact()` function to the view. Also I have been following [this laracast](https://laracasts.com/series/laravel-5-fundamentals/episodes/9). – ikartik90 Apr 01 '15 at 09:06
  • 1
    @ikartik90 This answer is totally correct. `lists()` is a method that you can call on collections. It will return an array that consists of a value (and optionally a key) from your model – lukasgeiter Apr 01 '15 at 09:14
  • Thanks for the answer Terry. It worked for me. But I'm still not clear on why my approach won't work when it works for Jeff in the laracast. And if I've understood it correctly, `lists` is a `value, key` pair and not a `key, value` pair, right? – ikartik90 Apr 01 '15 at 09:27
  • How am I supposed to create a dropdown list like this: `echo Form::select('animal', array( 'Cats' => array('leopard' => 'Leopard'), 'Dogs' => array('spaniel' => 'Spaniel'), ));` – ikartik90 Apr 01 '15 at 10:57
  • @ikartik90 you are already doing fine with the syntax above, the above multi-dimensional array will output dropdownlist with optgroup or are you asking for how to organize your array to the format above ? – terry low Apr 01 '15 at 11:43
  • @terrylow I wish to organize the array in the above format. – ikartik90 Apr 01 '15 at 14:12
  • with the model of this question ?? you might want to visit this page, this might come in handy with your case, #http://laravel.io/forum/02-07-2014-macro-formselect-with-optgroups-based-on-models-field – terry low Apr 01 '15 at 16:26
0

Try this..

In Controller:

$services = service_type::lists('location_area', 'location_id');

return view('home.index', compact('locations','services'));

In your blade:

 {{ Form::select('location',$locations,null, array('class'=> 'form-control'))}} 
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50