I found the fix. By using
Livewire makes sure the input fields are refreshed.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey everyone! I am new to livewire and have been experimenting with creating a flow for answering questions (in an audit form). I have set up the following code, which works with the DB and everything. However, after filling in rule 1 (which of course is empty) these values are presented in the form for rule 2. Whenever I "overwrite" them, these values are updated in the DB but for every rule I want the values on the form to be reset. Does anyone have any idea how to fix?
My livewire component has:
namespace App\Livewire;
use Livewire\Component; use App\Models\AuditResult; use App\Models\AuditRule; use App\Models\Audit;
class AuditWizard extends Component { public $audit; // Add this property to hold the current audit public $rules; // All audit rules public $currentRuleIndex = 0; // Tracks the current rule public $result; // Tracks the current result fields
protected $listeners = ['refresh' => '$refresh'];
public function mount($auditId)
{
// Load the audit along with related rules
$this->audit = Audit::findOrFail($auditId);
// Load all rules associated with this audit
$this->rules = AuditRule::all();
if ($this->rules->isEmpty()) {
abort(404, 'No rules found.');
}
$this->loadCurrentResult();
}
public function loadCurrentResult()
{
$currentRule = $this->rules[$this->currentRuleIndex];
// Fetch or initialize the result for the current rule
$existingResult = AuditResult::where('audit_rule_id', $currentRule->id)
->where('audit_id', $this->audit->id)
->first();
$this->result = [
'approved' => $existingResult->approved ?? null,
'comment' => $existingResult->comment ?? '',
'urgency' => $existingResult->urgency ?? 'normal',
'remediation' => $existingResult->remediation ?? '',
];
}
public function saveResult()
{
$currentRule = $this->rules[$this->currentRuleIndex];
// Save the current result to the database
AuditResult::updateOrCreate(
[
'audit_rule_id' => $currentRule->id,
'audit_id' => $this->audit->id,
],
$this->result
);
// Move to the next rule if possible
if ($this->currentRuleIndex + 1 < $this->rules->count()) {
$this->currentRuleIndex++;
$this->loadCurrentResult();
} else {
session()->flash('success', 'You have completed the AuditWizard!');
}
}
public function previousRule()
{
// Navigate to the previous rule if possible
if ($this->currentRuleIndex > 0) {
$this->currentRuleIndex--;
$this->loadCurrentResult();
}
}
public function render()
{
return view('livewire.audit-wizard', [
'currentRule' => $this->rules[$this->currentRuleIndex],
'isLastRule' => $this->currentRuleIndex === $this->rules->count() - 1,
])->extends('layouts.app');
}
}
And it has this blade view:
<div class="text-center mb-6">
<p><strong>Rule {{ $currentRuleIndex + 1 }} of {{ $rules->count() }}</strong></p>
<h2 class="text-xl font-bold">{{ $currentRule->title }}</h2>
<p class="text-gray-600">{{ $currentRule->description }}</p>
</div>
<form wire:submit.prevent="saveResult">
<div class="mb-4">
<label class="block font-bold mb-1">Approved:</label>
<select wire:model="result.approved" class="border p-2 w-full">
<option value="">Select</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
<div class="mb-4">
<label class="block font-bold mb-1">Comment:</label>
<textarea
wire:model="result.comment"
placeholder="Add your comment here..."
class="border p-2 w-full"
></textarea>
</div>
<div class="mb-4">
<label class="block font-bold mb-1">Urgency:</label>
<select wire:model="result.urgency" class="border p-2 w-full">
<option value="low">Low</option>
<option value="normal">Normal</option>
<option value="high">High</option>
</select>
</div>
<div class="mb-4">
<label class="block font-bold mb-1">Remediation:</label>
<textarea
wire:model="result.remediation"
placeholder="Add remediation steps here..."
class="border p-2 w-full"
></textarea>
</div>
<div class="flex justify-between">
@if ($currentRuleIndex > 0)
<button
type="button"
wire:click="previousRule"
class="bg-gray-500 text-white px-4 py-2 rounded"
>
Previous
</button>
@endif
<button wire:click="$refresh"
type="submit"
class="bg-blue-500 text-white px-4 py-2 rounded"
>
{{ $isLastRule ? 'Finish' : 'Next' }}
</button>
</div>
</form>
Please or to participate in this conversation.