Hamza Abdaoui
Hamza Abdaoui

Reputation: 2209

Blade : select option inside foreach loop

Having a collection of departments returned from DB.

I'm trying to display this list within a select in my .blade file, like this :

<select name="department" data-required="1" class="form-control">
     @foreach ($departments as $dep)
           <option value="{{ $dep->id }}" > {{ $dep->label }}</option>
     @endforeach
</select>

And now I am getting this error :

syntax error, unexpected '$dep' (T_VARIABLE), expecting ',' or ')' (View: /....../...blade.php) In the option line

enter image description here

But if I remove the "{{ $dep->id }}" from the value attribute of the option, everything works fine !

I am unable to figure it out ! And I am sure that I have an id attribute.

Thanks.

Upvotes: 1

Views: 2316

Answers (2)

Oussema Aroua
Oussema Aroua

Reputation: 5339

Just Remove the "" from this line :

 <option value={{ $dep->id }} > {{ $dep->label }}</option>

Upvotes: 3

Maulik Shah
Maulik Shah

Reputation: 1050

It might happen because of your view cache so Try after clearing your view cache, Because your code is perfect and it should work. Try clearing your view cache

php artisan view:clear

php artisan view:cache

To clear other cache

php artisan cache:clear

php artisan view:clear

php artisan view:cache

php artisan config:cache

Upvotes: 0

Related Questions