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

vincent15000's avatar

Livewire and hidden fields in models

Hello,

Why are the hidden fields exposed when retrieving a model via Livewire ?

For example : by using User::find(4);, I get the password while it's declared in the hidden fields.

I'm interested in knowing why.

Thanks for your answer ;).

V

0 likes
8 replies
LaryAI's avatar
Level 58

When using Livewire, it's important to understand how data is serialized and passed to the frontend. In Laravel, the $hidden property on a model is used to hide attributes when the model is converted to an array or JSON. However, Livewire doesn't automatically respect the $hidden property when it serializes models for frontend use.

Here's why this happens and how you can address it:

Why Hidden Fields Are Exposed

  1. Serialization Process: Livewire serializes the entire model object to pass it to the frontend. This serialization process doesn't automatically respect the $hidden property because it involves converting the model to a format that can be used in JavaScript, which is different from the typical JSON serialization.

  2. Direct Access: When you access a model directly in Livewire, it bypasses the typical JSON serialization process that respects the $hidden property.

Solution

To ensure that hidden fields are not exposed, you can manually control what data is passed to the frontend. Here are a couple of approaches:

1. Use toArray() or toJson()

Instead of passing the entire model to the frontend, convert the model to an array or JSON, which respects the $hidden property:

public function getUser($id)
{
    $user = User::find($id);

    // Convert to array or JSON to respect hidden fields
    return $user->toArray();
}

2. Define a Custom Method

Create a method in your model that returns only the fields you want to expose:

class User extends Model
{
    protected $hidden = ['password', 'remember_token'];

    public function toLivewireArray()
    {
        return $this->only(['id', 'name', 'email']); // Specify fields to expose
    }
}

Then, use this method in your Livewire component:

public function getUser($id)
{
    $user = User::find($id);

    return $user->toLivewireArray();
}

By using these approaches, you can ensure that sensitive fields like passwords are not exposed when using Livewire.

bvfi-dev's avatar

Would this help? Basically try:

protected $hidden = ['password'];

in your User Model

1 like
vincent15000's avatar

@bvfi-dev That's exactly the current situation. Some fields are declared hidden and are exposed via the serialization made by Livewire.

This means that this protected property isn't taken into account by Livewire.

1 like
Snapey's avatar

@vincent15000 its bad for both performance and security to place the entire user model (or any model) in a public property. The whole model will be shared with the frontend.

Better to only share the attributes that will be interracted with by the client

3 likes
vincent15000's avatar

@Snapey So passing the id is better than passing the model to the Livewire component.

That's strange that the Livewire documentation gives examples where it passes the entire model to the component.

Anybody could think that the documentation learns the best practice ...

Snapey's avatar
Snapey
Best Answer
Level 122

@vincent15000 nothing to stop you taking that model and mapping just the fields you want, or using toArray as has been suggested

$this->user = $user->toArray();

or

$this->username = $user->username;
$this->email = $user->email;
1 like
jj15's avatar

@Snapey

its bad for both performance and security to place the entire user model (or any model) in a public property. The whole model will be shared with the frontend.

I can understand the performance implications. But security is where I'm a little confused.

My understanding is that Livewire only sends the model's class (or morphable) name, primary key, and the same of any loaded relationships to/from the browser. Which is then used to re-query that model server-side. Where might it serialize and reveal hidden model attributes to the frontend?

Thank you.

1 like

Please or to participate in this conversation.