tzcoding
tzcoding

Reputation: 67

Whoops! undefined offset: 1 error laravel app

I'm making an app with laravel, it works incredibly well but when I try to get some extra data from a different table in my index method, then try to "foreach" it, it doesn't work. Here's the code:

controller:

$breeds= DB::table('dog_breed')->get();
return view('apps.dogs.index', ['dogs' => $dogs], ['breeds' => $breeds]);

index.blade.php (form):

<select aria-describedby="helpText" name="dog_breed" class="form-control">
   <option selected disabled>Pick Breed</option>
        @foreach $breeds as $breed
              <option value="did{{$breed->id}}">{{$breed->name}}</option>
        @endforeach
</select>

This is the error:

Undefined offset: 1

Thanks.

Upvotes: 0

Views: 117

Answers (2)

PHP Geek
PHP Geek

Reputation: 4033

You can use Wrong syntax or we can say that wrong method to write foreach loop .

@foreach ($breeds as $breed)
              <option value="did{{$breed->id}}">{{$breed->name}}</option>
        @endforeach

use rounde brases () in foreach loop

Upvotes: 0

Shihab EK
Shihab EK

Reputation: 70

Foreach derivatie syntax in Blade

@foreach($items as $item)

  //Do something with $item

@endforeach

Try

<select aria-describedby="helpText" name="dog_breed" class="form-control">
   <option selected disabled>Pick Breed</option>
        @foreach($breeds as $breed)
              <option value="did{{$breed->id}}">{{$breed->name}}</option>
        @endforeach
</select>

Upvotes: 1

Related Questions