2

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?

4

3 回答 3

2

您的实现的问题是,在选择框中,您在刀片中回显的是 a string,而不是object,因此线模态将通过获取输入事件的值并将其与后端同步来完成其工作。有两种方法可以实现您的目标。

  1. 将 select 选项的值作为制造商 id,然后您可以直接在更新的生命周期挂钩中使用它。

 <label for="manufacturer_id">Manufacturer</label>
    <select wire:model="manufacturer_id" id="manufacturer_id">
        <option selected="selected" disabled>Select a Manufacturer</option>
        @foreach($manufacturers as $selectableManufacturer)
        <option value="{{ $selectableManufacturer->id }}">{{ $selectableManufacturer->name }}</option>
        @endforeach
    </select>

在更新的钩子中,

 
 public $manufacturer_id; 
 public function updated() 
    {
        $this->cars = Car::where('manufacturer_id', $this->manufacturer_id)->get();
    }

  1. 或者只是使用该json_decode()函数使其成为一个对象,然后像您所做的那样访问 id 属性。

 public function updated()
    {
        $manufacturer = json_decode($this->manufacturer);
        $this->cars = Car::where('manufacturer_id', $manufacturer->id)->get();
    }

但健康的方法是使用 id 作为值。

于 2020-06-28T11:29:49.500 回答
1

您应该同步id所选制造商的并在更新的挂钩中使用它。这是你如何做到的。

class PostComponent extends Component
{
    public $manufacturers;
    public $manufacturer_id;
    public $manufacturer = 'Select a Manufacturer';
    public $cars;

    public function mount()
    {
        $this->manufacturers = Manufacturer::orderBy('name')->get(); 
    }
    
    public function updatedManufacturerId()
    {
        $this->cars = Car::where('manufacturer_id', $this->manufacturer_id)->get();
    }

    public function render()
    {
        return view('livewire.post-component');
    }
}

前端可以保持不变。

    <label for="manufacturer_id">Manufacturer</label>
    <select wire:model="manufacturer_id" id="manufacturer_id">
        <option selected="selected" disabled>Select a Manufacturer</option>
        @foreach($manufacturers as $selectableManufacturer)
        <option value="{{ $selectableManufacturer->id }}">{{ $selectableManufacturer->name }}</option>
        @endforeach
    </select>
于 2020-06-28T12:00:32.137 回答
1

只需按照文档进行操作即可:

https://laravel-livewire.com/docs/2.x/properties#binding-models

于 2020-11-09T01:59:48.867 回答