Binding nested data with eloquent model attributes

Binding nested data currently works for arrays. Would it be possible to allow nesting data with in eloquent model instance?

In my example below, I would like to be able to bind wire:model="contract_form.title" or wire:model="contract_form.date"

Obviously, I can make the eloquent model an array, but then I would lose the flexibility of using the instance in other methods (i.e. update())

ContractForm.php component

<?php

namespace App\Http\Livewire\Admin\Contracts;

use App\ContractForm;
use Livewire\Component;

class ViewContractForm extends Component
{
    public $contract_form;

    public function mount(ContractForm $contract_form)
    {
        $this->contract_form = $contract_form;
    }

    public function render()
    {
        return view('livewire.admin.contracts.view-contract-form');
    }
}

I’m guessing the answer is no because it would add too much overhead, but I thought I would ask.

Thanks!

I think, you are missing one additional step: validation.

Unfortunately, I don’t think this is possible because of how javascript handles objects. PHP objects aren’t javascript readable so they have to be serialized into a context that javascript CAN read - arrays. So you have to be more explicit with your models and data types which isn’t necessarily a bad thing. Just need to work within the limitation of the framework (and ultimately, the languages).

I only included what I thought was relevant. I’m not having an issue with validation.

1 Like

Thanks @xxdalexx I don’t know how I missed that searching. I’ve done some refactoring and I’m content now.