Keep your data secure with /** @locked */ in Laravel Livewire v3

In web applications, maintaining data integrity and security is crucial. Laravel Livewire v3 introduces a powerful feature, the /** @locked */ annotation, which helps prevent unauthorized changes to your component properties. This ensures that your sensitive data remains secure and consistent. Let’s explore how to use /** @locked */ in your Laravel Livewire projects.

Understanding /** @locked */

The /** @locked */ annotation is used to protect component properties from being altered unintentionally or by unauthorized actions. When applied to a property, it ensures that the value of this property cannot be modified during the component's lifecycle, thereby maintaining its integrity.

Basic Usage

Here’s a basic example to illustrate how /** @locked */ works:

use Livewire\Component;

class SecureComponent extends Component
{
    /** @locked */
    public $protectedProperty = 'Sensitive Data';

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

In this example, the protectedProperty is annotated with /** @locked */, ensuring that its value remains 'Sensitive Data' throughout the component's lifecycle.

Real-Life Example

Consider a scenario where you are developing an admin dashboard that displays sensitive information, such as user roles or access permissions. You want to ensure that these properties are not altered by any component interaction. Here’s how you can implement this using /** @locked */:

use Livewire\Component;

class AdminDashboard extends Component
{
    /** @locked */
    public $userRole = 'admin';

    public $otherData;

    public function loadData()
    {
        $this->otherData = SomeModel::all();
    }

    public function render()
    {
        return view('livewire.admin-dashboard');
    }
}
<!-- Blade Template (livewire/admin-dashboard.blade.php) -->
<div>
    <h1>Admin Dashboard</h1>
    <p>User Role: {{ $userRole }}</p>
    <button wire:click="loadData">Load Data</button>

    <div>
        @if($otherData)
            @foreach($otherData as $data)
                <p>{{ $data->name }}</p>
            @endforeach
        @endif
    </div>
</div>

In this example, the userRole property is annotated with /** @locked */, ensuring it remains 'admin' regardless of other component interactions. This protects the role information from being changed accidentally or maliciously.

Benefits of Using /** @locked */

  • Enhanced Security: Prevent unauthorized changes to critical properties, ensuring sensitive data remains protected.
  • Data Integrity: Maintain consistent values for important properties throughout the component's lifecycle.
  • Simplified Management: Easily protect specific properties without needing additional validation or checks.

Conclusion

The /** @locked */ annotation in Laravel Livewire v3 is a powerful tool for enhancing data security and integrity in your applications. By leveraging this feature, you can ensure that critical properties remain unchanged, protecting sensitive information and maintaining consistency.

Subscribe to Harris Raftopoulos

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe