Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

valentin_vranic's avatar

Livewire wire:model with Model relationship

Hi guys!

Laravel: 10:48.12 Livewire: 3.4

How is it possible to bind model relation to input? I have HasOne relation loaded to model.

I'm trying something like this (but it doesn't recognize it) :

<input type="checkbox" wire:model="callityRelation.checked"
                   wire:change="changeState"> 

callityRelation is the name of the relation, and it is loaded properly.

Thanks in advance!

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To bind a model relationship to an input in Livewire, you need to ensure that the relationship is properly loaded and that you are referencing it correctly in your Livewire component. Here’s a step-by-step solution to achieve this:

  1. Ensure the Relationship is Loaded: Make sure that the HasOne relationship is loaded in your model. You can use the with method to eager load the relationship.

  2. Livewire Component: In your Livewire component, you need to define the property that will hold the related model data and ensure it is properly initialized.

  3. Blade Template: Use the wire:model directive to bind the input to the related model property.

Here’s an example to illustrate this:

Step 1: Define the Relationship in Your Model

Assuming you have a User model with a HasOne relationship to a Profile model:

// User.php
class User extends Model
{
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

Step 2: Load the Relationship in the Livewire Component

In your Livewire component, load the relationship and define a property to hold the related model data:

// UserProfileComponent.php
use Livewire\Component;

class UserProfileComponent extends Component
{
    public $user;
    public $profile;

    public function mount(User $user)
    {
        $this->user = $user->load('profile');
        $this->profile = $this->user->profile;
    }

    public function changeState()
    {
        // Handle the change state logic here
    }

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

Step 3: Bind the Input in the Blade Template

In your Blade template, use the wire:model directive to bind the input to the related model property:

<!-- user-profile-component.blade.php -->
<div>
    <input type="text" wire:model="profile.some_field" wire:change="changeState">
</div>

Step 4: Update the Related Model

Ensure that any changes to the input are saved back to the related model. You can do this in the changeState method or another appropriate method in your Livewire component:

// UserProfileComponent.php
public function changeState()
{
    $this->profile->save();
}

Full Example

UserProfileComponent.php:

use Livewire\Component;

class UserProfileComponent extends Component
{
    public $user;
    public $profile;

    public function mount(User $user)
    {
        $this->user = $user->load('profile');
        $this->profile = $this->user->profile;
    }

    public function changeState()
    {
        $this->profile->save();
    }

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

user-profile-component.blade.php:

<div>
    <input type="text" wire:model="profile.some_field" wire:change="changeState">
</div>

This setup ensures that the input field is bound to the some_field attribute of the Profile model, and any changes to the input will trigger the changeState method, which saves the updated profile data.

1 like

Please or to participate in this conversation.