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

intrithm's avatar

Livewire with Eloquent

I've edit model for users ,

$this->user= User::find(1);

In my html i cannot access $user properties why?

wire:model="user.name" // giving empty

Do i need to ser name,dob and all other user properites individually ?

0 likes
17 replies
intrithm's avatar

@Tray2 yes but i have like 15 fields.. I don’t want to create each variable for each of my database Field

1 like
intrithm's avatar

@Tray2 yeah yes i did that but nothing is working.if i enter some name in input ,it gets deleted and nulled , if i set like

$this->user->name = 'David';

This Also not working

1 like
Chingy's avatar

@Tray2 definitely not, that would be a single public property, aside the $user one

2 likes
Tray2's avatar

@Chingy In Livewire 3 yes, but in Livewire 2 that worked, and if you read later replies, you see that it can be turned on in Livewire 3 as well.

2 likes
tykus's avatar

If you want the legacy behaviour, then you need to enable it in the livewire config file

In Livewire 3, binding directly to Eloquent models has been disabled in favor of using individual properties, or extracting Form Objects.

However, because this behavior is so heavily relied upon in Livewire applications, version 3 maintains support for this behavior via a configuration item in config/livewire.php:

'legacy_model_binding' => true, By setting legacy_model_binding to true, Livewire will handle Eloquent model properties exactly as it did in version 2.

https://livewire.laravel.com/docs/upgrading#eloquent-model-binding

2 likes
Snapey's avatar

Be careful with this approach. Anything bound to a Livewire public propertiy is visible to the client.

Mark sure you don't expose something that is meant to be private.

The fact that this approach has been deprecated should tell you something.

Besides, listing the fields is quicker than participating in this thread.

4 likes
Tray2's avatar

@Snapey One of the reasons I don't like livewire, it goes against everything I learned about properties and OOP, but it's cool that it is so easy to do, but setting properties to public makes my skin crawl.

3 likes
jlrdw's avatar

@Tray2

One of the reasons I don't like livewire ...

Plus 100.

To me it's just as easy using regular mvc with blade, and axios js.

3 likes
Snapey's avatar

@Tray2 thats a bit irrational since its exactly what you would do in a json resource where you can happily send the whole model to the client as json.

If someone exposed private data doing this, you would say, you need to think carefully about what properties you expose in the json response. This is no different except in the mind of the developer who is used to class properties to be server side only.

2 likes
Snapey's avatar

@jlrdw you should try it. Takes avay both ends of writing an ajax API

3 likes
jlrdw's avatar

@Snapey I did try and played with it some, but I prefer server fetched partials for things like lookup tables.

I.e., picking which carrier for a load.

It's basically still ajax, but happening behind the scenes.

2 likes
Tray2's avatar

@Snapey I know, but I'm old and have been drilled that properties should never be exposed like that in a class, they should always have getters and setters. I'm just a bit conservative I guess when it comes to how a class should be written.

2 likes
Muhammad_Nawaz's avatar

The reason wire:model="user.name" is giving you an empty value is that Livewire doesn't automatically bind complex objects like Eloquent models directly to the component state. By default, Livewire expects the properties you want to bind to be public properties or explicitly defined.

Solution You have two main options to properly bind the user properties:

  1. Use wire:model for Each Property Individually You can define each property of the user model you want to bind individually in the Livewire component. For example: class EditUser extends Component { public $name; public $dob;

    public function mount() { $user = User::find(1); $this->name = $user->name; $this->dob = $user->dob; }

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

Then in your Blade template: 2. Use wire:model.defer with Eloquent Model Properties If you want to work directly with the Eloquent model, use wire:model.defer to prevent immediate updates and work with the model as a whole class EditUser extends Component { public $user;

public function mount()
{
    $this->user = User::find(1);
}

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

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

} In your Blade template: Save

let me know if you have any confusion

2 likes
vincent15000's avatar

If you are using Livewire v3, I suggest you to use a Livewire Form.

It works fine wigth wire:model="user.name".

1 like

Please or to participate in this conversation.