How can I pass a hidden form input in Laravel Livewire?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How Can I Pass Hidden Form Inputs in Laravel Livewire? A Deep Dive
Implementing dynamic forms with Laravel Livewire is incredibly powerful, allowing you to build rich, interactive user interfaces without writing extensive JavaScript. However, as you've encountered, managing data flow—especially hidden inputs that influence the backend logic—can sometimes be tricky. The issue often lies not in how you define the HTML, but in how Livewire exposes that data to its server-side methods upon submission.
This post will walk you through the correct, robust way to manage and submit hidden form input values within a Laravel Livewire component, ensuring your data flows seamlessly from the frontend to the backend.
Understanding the Livewire Data Flow
Livewire operates on a reactive principle. When you use wire:model, Livewire automatically synchronizes the DOM elements with the properties of your PHP component. For hidden inputs to be successfully submitted, their values must be part of the data payload that is sent when the form is submitted (usually via wire:submit or an action method).
The reason you might not be receiving values from inputs like rating_val and reviewable_id is often related to how you are calling your submission method or if those variables are being treated purely as visual elements rather than data sources.
The Correct Approach: Binding Hidden Inputs
The solution involves ensuring that every piece of data intended for the server—whether visible or hidden—is explicitly bound to a public property within your Livewire component class.
Consider the example structure you provided:
<input type="text" class="w-100 ratings-hidden" value="" wire:model="rating_val">
<input class="" value="" wire:model="reviewable_id" type="hidden">
<textarea class="form-control w-100 animated" cols="50" id="new-review" wire:model="comment" placeholder="Enter your review here..." rows="5"></textarea>
In this scenario, the key is that Livewire automatically manages rating_val and reviewable_id because they are bound via wire:model. If you are using a standard HTML <form> element to trigger the submission, ensure that the form itself is correctly defined and targets the necessary action.
Step-by-Step Implementation
- Define Component Properties: Make sure the variables you need (
rating_val,reviewable_id,comment) are public properties in your Livewire component class (e.g.,RatingForm.php). - Bind Everything: Use
wire:modelfor all inputs, including those you intend to keep hidden. This ensures the values are automatically synced with the component state. - Action Method: When you trigger the submission (e.g., using
wire:submitor a standard form POST), ensure your corresponding method correctly receives these properties.
Here is how the Livewire interaction typically looks in your component class:
// app/Http/Livewire/RatingForm.php
use Livewire\Component;
class RatingForm extends Component
{
// These properties automatically sync with the inputs via wire:model
public $rating_val;
public $reviewable_id;
public $comment;
public function submitReview()
{
// When this method runs, it has access to all bound properties.
$data = [
'rating' => $this->rating_val,
'reviewable_id' => $this->reviewable_id, // This hidden value is now accessible!
'comment' => $this->comment,
];
// Logic to save data to the database using Eloquent or a Repository.
// For example: Review::create($data);
session()->flash('message', 'Review submitted successfully!');
}
public function render()
{
return view('livewire.rating-form');
}
}
Best Practices for Hidden Data Submission
While the above method works perfectly, there are a few architectural considerations to keep in mind when dealing with complex data submissions in Laravel applications:
- Avoid Purely Static Hidden Fields: If an input is only a static ID that doesn't change based on user interaction (like a foreign key), consider passing this information via Livewire component properties rather than relying solely on the DOM structure, especially as your application grows.
- Use Dedicated Form Objects: For complex submissions, encapsulate related data into a single DTO or model before submitting it to ensure type safety and clarity. This aligns well with Laravel's strong object-oriented approach, which is core to building robust applications on the Laravel framework.
- Leverage Eloquent Relationships: When dealing with IDs (like
reviewable_id), always rely on Eloquent relationships rather than manually passing raw IDs if possible. This keeps your data integrity managed by the ORM, simplifying the logic within your Livewire methods.
Conclusion
Passing hidden form inputs in Laravel Livewire is straightforward once you understand that Livewire synchronizes bound properties to the component state. By ensuring that all necessary values—even those intended to be hidden—are explicitly declared as public properties and bound via wire:model, you guarantee that these values are available within your submission methods. Mastering this data flow is key to building efficient, reactive interfaces with Livewire.