如何将 Laravel Livewire 与 Eloquent 模型结合使用?

我正在第一次在项目中尝试 Laravel Livewire。我正在尝试构建一个相当简单的表单,其中包含一组级联下拉菜单 - 一个输入依赖于另一个输入。但是,当我尝试将雄辩的对象传递给组件属性时,它会转换为字符串,因此我无法访问该对象的任何属性。


我觉得我可能缺少一些非常简单的东西。


这是我的组件代码:


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');

    }

这是我的刀片文件:


<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>

当我尝试在下拉列表中选择制造商时,我可以记录$manufacturer并看到该值是


{"id":16,"name":"Manufacturer1","description":"lorem ipsum","created_at":"2020-06-26T23:44:37.000000Z","updated_at":"2020-06-26T23:44:37.000000Z"}  


但是,当我尝试在生命周期挂钩中选择汽车时尝试获取id制造商的信息时updated,出现错误


尝试获取非对象的属性“id”


知道为什么会发生这种情况以及如何解决它吗?


缥缈止盈
浏览 143回答 3
3回答

MMMHUHU

您的实现的问题是,在选择框中您在刀片中回显的是string,而不是object,因此线模式将通过获取输入事件的值并将其与后端同步来完成其工作。有两种方法可以实现您的目标。将 select 选项的值作为制造商 id,然后您可以直接在更新的生命周期挂钩中使用它。&nbsp;<label for="manufacturer_id">Manufacturer</label>&nbsp; &nbsp; <select wire:model="manufacturer_id" id="manufacturer_id">&nbsp; &nbsp; &nbsp; &nbsp; <option selected="selected" disabled>Select a Manufacturer</option>&nbsp; &nbsp; &nbsp; &nbsp; @foreach($manufacturers as $selectableManufacturer)&nbsp; &nbsp; &nbsp; &nbsp; <option value="{{ $selectableManufacturer->id }}">{{ $selectableManufacturer->name }}</option>&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </select>在更新的钩子中,&nbsp;&nbsp;public $manufacturer_id;&nbsp;&nbsp;public function updated()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->cars = Car::where('manufacturer_id', $this->manufacturer_id)->get();&nbsp; &nbsp; }或者只是使用该json_decode()函数使其成为一个对象,然后像您所做的那样访问 id 属性。&nbsp;public function updated()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $manufacturer = json_decode($this->manufacturer);&nbsp; &nbsp; &nbsp; &nbsp; $this->cars = Car::where('manufacturer_id', $manufacturer->id)->get();&nbsp; &nbsp; }但健康的方法是使用 id 作为值。

精慕HU

您应该同步id所选制造商的 并在更新的挂钩中使用它。以下是您可以如何做到这一点。class PostComponent extends Component{&nbsp; &nbsp; public $manufacturers;&nbsp; &nbsp; public $manufacturer_id;&nbsp; &nbsp; public $manufacturer = 'Select a Manufacturer';&nbsp; &nbsp; public $cars;&nbsp; &nbsp; public function mount()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->manufacturers = Manufacturer::orderBy('name')->get();&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public function updatedManufacturerId()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->cars = Car::where('manufacturer_id', $this->manufacturer_id)->get();&nbsp; &nbsp; }&nbsp; &nbsp; public function render()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return view('livewire.post-component');&nbsp; &nbsp; }}前端可以保持不变。&nbsp; &nbsp; <label for="manufacturer_id">Manufacturer</label>&nbsp; &nbsp; <select wire:model="manufacturer_id" id="manufacturer_id">&nbsp; &nbsp; &nbsp; &nbsp; <option selected="selected" disabled>Select a Manufacturer</option>&nbsp; &nbsp; &nbsp; &nbsp; @foreach($manufacturers as $selectableManufacturer)&nbsp; &nbsp; &nbsp; &nbsp; <option value="{{ $selectableManufacturer->id }}">{{ $selectableManufacturer->name }}</option>&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </select>

慕哥9229398

只需按照文档进行操作即可:https://laravel-livewire.com/docs/2.x/properties#binding-models
打开App,查看更多内容
随时随地看视频慕课网APP