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

javguerra's avatar

Error when I use livewire to get the value of a select generated by @foreach

I want to show an input field when I select a concrete option in a select.

This code works,

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Nia extends Component
{
    public $roles;
    public $role;
    public $show = false;

    public function change()
    {
        $this->role == 2 ? $this->show = true : $this->show = false;
    }

    public function render()
    {
        return <<<'blade'
                <div class="col-span-6 sm:col-span-4 grid gap-6">
                <div class="lg:flex grid gap-6">
                    <!-- Rol -->
                    <div class="col-span-6 lg:col-span-2 w-full">
                        <x-jet-label for="role" value="{{ __('Role') }}" />
                        <select id="role" name="role" wire:model="role" wire:change="change"
                            class="mt-1 block w-full border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm">
                            <option value="">{{__('Choose an option')}}</option>
                            <option value="0" {{old('role') == 0 ? 'selected' : '' }}>{{__('Admin')}}</option>
                            <option value="1" {{old('role') == 1 ? 'selected' : '' }}>{{__('Teacher')}}</option>
                            <option value="2" {{old('role') == 2 ? 'selected' : '' }}>{{__('Student')}}</option>
                        </select>
                        <x-jet-input-error for="role" class="mt-2" />
                    </div>
                    <!-- NIA -->
                    <div class="col-span-6 lg:col-span-2 w-full">
                        @if($show)
                            <x-jet-label for="nia" value="{{ __('Student identification number') }}" />
                            <x-jet-input id="nia" name="nia" type="text" class="mt-1 block w-full" value="{{ old('nia') }}" required="required" minlength="7" />
                            <x-jet-input-error for="nia" class="mt-2" />
                        @endif
                    </div>
                </div>
            </div>
        blade;
    }
}

but if I change the select for this:

<select id="role" name="role" wire:model="role" wire:change="change"
    class="mt-1 block w-full border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm">
    <option value="">{{__('Choose an option')}}</option>
    @foreach($roles as $rol)
         <option value="{{$rol->id}}" {{old('role') == $rol->id ? 'selected' : '' }}>{{__($rol->name)}}</option>
    @endforeach
</select>

when I use @foreach I get the following error:

ErrorException Trying to get property 'id' of non-object

I don't really understand the problem. Can someone explain to me where the problem may be or put me on track to further investigate?

0 likes
5 replies
Tray2's avatar

It tells you that the the array of roles does not contain an object. So do a dd on roles and see what it contains.

javguerra's avatar

Hi Tray2,

Illuminate\Support\Collection {#1496 ▼
  #items: array:3 [▼
    0 => {#1492 ▼
      +"id": "0"
      +"name": "Admin"
    }
    1 => {#1494 ▼
      +"id": "1"
      +"name": "Teacher"
    }
    2 => {#1495 ▼
      +"id": "2"
      +"name": "Student"
    }
  ]
}

Which was created to be used as a view composer like this:

        $roles = collect([
            (object) [ 'id' =>  '0', 'name' => 'Admin'],
            (object) [ 'id' =>  '1', 'name' => 'Teacher'],
            (object) [ 'id' =>  '2', 'name' => 'Student'],
        ]);

and I call the livewire component as follows:

<livewire:nia :roles="$roles" />
Snapey's avatar

you don't need to use old or selected in a wire:model select

javguerra's avatar

Thanks Snapey. I am just taking my first steps with Livewire. Could you give me more information or a link where I can consult this information related to the 'old' and 'selected'?

javguerra's avatar

Okay. I have been doing tests, and I see that the model feeds the value of the select, although if I should use the following to take the old value when other fields of the form fail.

    public function mount()
    {
        $this->role = old('role');
        $this->change();
    }

Thanks a lot for the suggestion.

But I still don't understand the error I get when I use @foreach ... Surely it will be a very simple thing, but I can't see it.

Please or to participate in this conversation.