What seems to be the problem:
I have a strange issue where I’m getting all the records (only 3 records) from the DB, but if in Laravel blade I do a foreach loop, the id
sets to 0 for all records.
If I echo out the object then all IDs are correct, it’s only when I add it into the foreach loop.
Steps to Reproduce:
// Livewire controller
public $regions;
public $region_id;
public function mount(){
$this->regions = Region::all();
$this->region_id = Auth::user()->settings->region_id;
}
<!-- Blade view -->
<div>
<label for="region" class="block text-sm font-medium text-gray-700">
Region
</label>
<div class="mt-1 rounded-md shadow-sm flex w-2/4">
<select wire:model="region" class="block w-full pl-3 pr-10 py-2 text-base border border-gray-300 focus:outline-none focus:ring-red-500 focus:border-red-500 sm:text-sm rounded-md">
<option>Please choose a region</option>
@foreach($regions as $region)
<option value="{{ $region->id }}" {{ $region->id = $region_id ? 'selected' : '' }}>{{ $region->title }}</option>
@endforeach
</select>
</div>
@error('region') <span class="text-xs text-red-600 error">{{ $message }}</span> @enderror
</div>
Output from the $regions variable
[{"id":1,"title":"in the UK","abbr":"A","region_code":"UK","created_at":"2018-12-30T21:47:39.000000Z","updated_at":"2018-12-30T21:47:39.000000Z"},{"id":2,"title":"in the EU","abbr":"B","region_code":"EU","created_at":"2018-12-30T21:47:39.000000Z","updated_at":"2018-12-30T21:47:39.000000Z"},{"id":3,"title":"outside of EU","abbr":"C","region_code":"ROW","created_at":"2018-12-30T21:47:39.000000Z","updated_at":"2018-12-30T21:47:39.000000Z"}]
Output when in the options input
[{“id”:0,“title”:“in the UK”,“abbr”:“A”,“region_code”:“UK”,“created_at”:“2018-12-30T21:47:39.000000Z”,“updated_at”:“2018-12-30T21:47:39.000000Z”},{“id”:0,“title”:“in the EU”,“abbr”:“B”,“region_code”:“EU”,“created_at”:“2018-12-30T21:47:39.000000Z”,“updated_at”:“2018-12-30T21:47:39.000000Z”},{“id”:0,“title”:“outside of EU”,“abbr”:“C”,“region_code”:“ROW”,“created_at”:“2018-12-30T21:47:39.000000Z”,“updated_at”:“2018-12-30T21:47:39.000000Z”}]
Are you using the latest version of Livewire:
Yes.
Any ideas?