I am in the process of trying out Laravel Livewire with a project for the first time. I am trying to build a fairly simple form with one set of cascading dropdowns - one input dependent on the other. But, when I try to pass an eloquent object to a component property, it is converted to a string so I can not access any of the object's properties.
I feel like there is something very simple I may be missing.
Here is my component code:
PostComponent.php
class PostComponent extends Component
{
public $manufacturers;
public $manufacturer = 'Select a Manufacturer';
public $cars;
public function mount()
{
$this->manufacturers = Manufacturer::orderBy('name')->get();
}
public function updated()
{
$this->cars = Car::where('manufacturer_id', $this->manufacturer->id)->get();
}
public function render()
{
return view('livewire.post-component');
}
Here is my blade file:
<label for="manufacturer">Manufacturer</label>
<select wire:model="manufacturer" id="manufacturer">
<option selected="selected" disabled>Select a Manufacturer</option>
@foreach($manufacturers as $selectableManufacturer)
<option value="{{ $selectableManufacturer }}">{{ $selectableManufacturer->name }}</option>
@endforeach
</select>
When I attempt to select a manufacturer in the dropdown, I can log the $manufacturer
and see that the value is
{"id":16,"name":"Manufacturer1","description":"lorem ipsum","created_at":"2020-06-26T23:44:37.000000Z","updated_at":"2020-06-26T23:44:37.000000Z"}
But when I try to get the id
of the manufacturer when attempting to select cars in the updated
lifecycle hook, I get the error
Trying to get property 'id' of non-object
Any idea as to why this is happening and how to fix it?