Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anonymouse703's avatar

Error: Invalid argument supplied for foreach()

I tried to loop a check box but I got an error. I already tried dd(); and there are data but in the display the error occurred.

this is my blade where the error pop-out

@foreach($roles as $role)
<div class="form-check">
    <input type="check" class="form-check-input"  value="{{$role->id}}" name="roles[]" id="{{$role->name}}" >   
    <label class="form-check-label" for="{{$role->name}}">{{$role->names}}</label>
</div>
@endforeach

this is my livewire.

 public function render()
    {
        $roles = Role::all();
        return view('livewire.user-form')
            ->with('roles',$roles);
    }
0 likes
8 replies
SilenceBringer's avatar

@anonymouse703 can you show the full error? Which type you actually have in foreach? Or dump it right before foreach and show output

@dd($roles)
@foreach($roles as $role)
<div class="form-check">
    <input type="check" class="form-check-input"  value="{{$role->id}}" name="roles[]" id="{{$role->name}}" >   
    <label class="form-check-label" for="{{$role->name}}">{{$role->names}}</label>
</div>
@endforeach
anonymouse703's avatar

this is the dump data where the I fetch all the roles

Illuminate\Database\Eloquent\Collection {#1448 ▼
  #items: array:5 [▼
    0 => Spatie\Permission\Models\Role {#1449 ▶}
    1 => Spatie\Permission\Models\Role {#1450 ▶}
    2 => Spatie\Permission\Models\Role {#1451 ▶}
    3 => Spatie\Permission\Models\Role {#1452 ▶}
    4 => Spatie\Permission\Models\Role {#1455 ▶}
  ]
}

sample item from the array

 #items: array:5 [▼
    0 => Spatie\Permission\Models\Role {#1449 ▼
      #guarded: array:1 [▶]
      #connection: "mysql"
      #table: "roles"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:5 [▶]
      #original: array:5 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      -permissionClass: null
    }

and this is the error

Invalid argument supplied for foreach() (View: C:\wamp64\www\axiebus_v2\resources\views\livewire\user-form.blade.php)

which pointed here

@foreach($roles as $role)
<div class="form-check">
    <input type="check" class="form-check-input"  value="{{$role->id}}" name="roles[]" id="{{$role->name}}" >   
    <label class="form-check-label" for="{{$role->name}}">{{$role->names}}</label>
</div>
@endforeach
SilenceBringer's avatar

@anonymouse703 in my first comment I asked you to dump roles right before foreach

@dd($roles)
@foreach($roles as $role)

did you do that?

anonymouse703's avatar

sorry sir, taking lunch. it's null but I passed it already in the view

public function render()
    {
        $roles = Role::get();
        // dd($roles);
        return view('livewire.user-form')
            ->with('roles',$roles);
    }
``

Please or to participate in this conversation.