having a small issue,
StudentTable
id adminno name class_id section_id
1 100 Abdul 1 1
2 101 Bazith 1 1
TermFeeTable
id FeeDetails amt class_id
1 Term1 2000 1
2 Term2 3000 1
3 Term3 5000 1
TermFeePaymentTable
id stud_id class_id section_id termfee_id paid_amt
1 1 1 1 1 2000
2 1 1 1 2 3000
Now i need to fetch class wise. that is when a class is clicked its relevant student with term fee from termfee table, and wether the fee paid by the student from termfeepayment table must be fetched,
my models
//StudentTable model
public function classdetails()
{
return $this->belongsTo('App\ClassDetails', 'class_id');
}
//TermFeeTable model
public function TermfeePaymentFromStudent()
{
// should filter student
return $this->hasMany(TermFeePayment::class, 'termfee_id');
}
i wrote this query. but some problem
$student = StudentTable::find($id);
$fees = TermFeeTable::query()->where('class_id', $student->class_id)
->with([
'TermfeePaymentFromStudent' => function ($query) use ($id) {
$query
->selectRaw('stud_id, SUM(paid_amt) as paid')
->where('stud_id', $id)
->groupBy(['stud_id', 'termfee_id']);
},
])
->get();
The above query works fine for single student. but i need to take a whole class with section, like if a choose V std A section its students with who paid and not paid must be retrieved
i tried like this
$students = StudentTable::where('stud_status','active')->where('class_id',$request->class_id)->orderby('stud_name','asc')->get();
foreach ($students as $student) {
$id=$student->id;
$admissions[] = TermFeeTable::query()->where('class_id', $student->class_id)
->with([
'TermfeePaymentFromStudent' => function ($query) use ($id) {
$query
->selectRaw('stud_id, termfee_id, SUM(paid_amt) as paid')
->where('stud_id', $id)
->groupBy(['stud_id', 'termfee_id']);
},
])
->get();
}
i think iam using two objects students and admissions.. the out put i expect is
Name adminno fee TotalAmt
Abdul 100 Term1-2000
Term2-3000
Term3-5000 10000
Bazith 101 Term1-2000
Term2-3000
Term3-5000 10000
if first boy paid one term means the output should be
Name adminno fee TotalAmt
Abdul 100 Term1-paid
Term2-3000
Term3-5000 8000
Bazith 101 Term1-2000
Term2-3000
Term3-5000 10000
My second query gives partial output the loop near the fee iam missing something
this is my blade file whic gives close to my output
<tbody>
@foreach ($students as $student)
<tr>
<td> {{ $student->stud_name }} </td>
<td> {{ $student->admission_no }} </td>
<td>
@php $total=0; @endphp
@foreach ($admissions as $admission)
@foreach ($admission as $admission12)
@if(count($admission12->TermfeePaymentFromStudent)==0)
{{ $admission12->FeeDetail }} - {{$admission12->Termamount }}
@else
@foreach($admission12->TermfeePaymentFromStudent as $feeee)
{{ $admission12->Termamount - ($feeee->paid) }}
@endforeach
@endif
<br>
@php $total= $admission12->Termamount + $total ; @endphp
@endforeach
</td>
<td> {{ $total }} </td>
</tr>
@endforeach
@endforeach
</tbody>
how to rectify it