why do you have a pivot table? I can understand a student might have many invoices, but why would an invoice have many students?
Jun 2, 2023
12
Level 1
Filter in Laravel Livewire Payment Component
Hello,
I have 3 models, Invoice , Student and Payment.
Invoice model is related to student model with many to many relationship and there is a pivot table, with invoice_id and student_id. The relationship name on invoice model is studentName.
In payment component view, I have a form as below:
<form wire:submit.prevent="filterInvoices" class="pt-3">
<div class="form-group">
<label class="form-label required" for="student_name">Name</label>
<x-select-list class="form-control" required
id="student_name" name="student_name"
:options="$this->listsForFields['student_name']"
wire:model="selectedStudent" />
<div class="validation-message">
{{ $errors->first('student_name') }}
</div>
</div>
<div class="form-group">
<button class="btn btn-indigo mr-2" type="submit">
Filter
</button>
<div>
<div>
@foreach ($invoices as $invoice)
<p>{{ $invoice->invoice_number }}</p>
@endforeach
</div>
</form>
In payment component class, I have this:
public array $invoice_no = [];
public $invoices = [];
public array $listsForFields = [];
//for filter card
public $selectedStudent;
public function filterInvoices()
{
$this->render();
}
public function render()
{
// Fetch all the students
$students = Student::all();
// Fetch invoices based on the selected student name
$invoices = Invoice::when($this->selectedStudent, function ($query) {
$query->whereHas('studentName', function ($q) {
$q->where('student_id', $this->selectedStudent);
});
})->get();
return view('livewire.payment.create', [
'students' => $students,
'invoices' => $invoices,
]);
}
When I click the submit button on the form, nothing happens, no filter is applied. Where my code is wrong.
Many thanks
Please or to participate in this conversation.