Home > Mobile >  Laravel Livewire update deep nested Model
Laravel Livewire update deep nested Model

Time:05-29

I have created a Livewire Form which includes couple of deep nested relations. How can I update this relations in livewire on submit?

Here is my trying:

class ProfileForm extends Component {

use WithFileUploads;

public $upload;
/**
 * @var
 */
public User $user;

/**
 * @var
 */
public $specialistations;

/**
 * @var
 */
public $occupationGroups;

/**
 * @var
 */
public $professionGroups;



protected array $rules = [
    'user.account.company' => ['integer', 'nullable'],
    'user.account.specialisations' => ['nullable', 'array'],
    'user.account.professions' => [],
    'user.account.occupations' => ['nullable', 'array'],
    'user.account.about' => ['string', 'nullable'],
    'user.account.company_addon' => ['nullable'],
    'user.account.company_phone' => ['nullable'],
    'user.account.company_email' => ['email', 'nullable'],
    'user.account.phone' => ['nullable'],
    'user.account.published' => ['nullable'],
    'user.account.web' => ['nullable'],
    'user.account.company_fax' => ['nullable'],
    'user.account.fax' => ['nullable'],
    'user.account.email' => ['email'],
    'upload.*' => 'image|max:1024',
];


/**
 * @return void
 */
public function mount()
{
    $user = auth()->user()->load(['account', 'account.specialisations']);
    $this->specialistations = Specialisation::get();
    $this->occupationGroups = OccupationGroup::get();
    $this->professionGroups = Profession::get();
    $this->user = $user;
}

public function submit()
{
    if($this->upload) {
        $this->user->avatar = $this->upload->store('files', 'public');
    }

    $this->user->update();
    $this->user->account->update();
}


public function render()
{
    return view('livewire.profile-form');
}

}

CodePudding user response:

As per Livewire docs - nested data, you can save the related model


public function submit()
{
    if($this->upload) {
        $this->user->avatar = $this->upload->store('files', 'public');
    }

    $this->validate();

    $this->user->save();
    $this->user->account->save();
}
Update: (For anyone visiting in future)

the problem is that $this->user->account is having other relations and the save will fail with SQLSTATE[42S22]: Column not found: 1054 Unknown column 'professions' in 'field list'

As pointed out by OP- @fefe himself, ensure that all the relations are loaded (all relations which are used, updated in the component).

In the mount() method

public function mount()
{
    $user = auth()->user()->load([
        'account', 
        'account.specialisations',
        'account.professions',
        'account.occupations',
    ]);

    $this->specialistations = Specialisation::get();
    $this->occupationGroups = OccupationGroup::get();
    $this->professionGroups = Profession::get();
    $this->user = $user;
}
Option Two (Better)

Split the component into two: one for user and another component for user account.

Then handle saving all the relations of account in UserAccount component. If the relations are nested only a single level deep then it should not be a problem.

  • Related