Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

how to get all student who paid the fee and not paid the fee. have solution, but expecting some what better because of loop issue

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

0 likes
6 replies
Snapey's avatar

You should focus on doing this with eloquent and not looping over the data in the controller.

But you seem to be missing a clear identification, what term is being paid. You have the words 'Term1' but not an actual term number.

Do you not have a table that represents terms?

AbdulBazith's avatar

@snapey thank you for your response

actually term1 here mentioned as just a name thats it

here what schools will do is just they will have a name Term1 -10000 fee like this only. there is no explanation or any numeric for Term1

just this is the only table

TermFeeTable

id	FeeDetails	amt	class_id
1	Term1		2000	1
2	Term2		3000	1
3	Term3		5000	1
//See this is for next class......
4	Term1		8000	2
5	Term2		15000	2
6	Term3		20000	2


for this class, for this term this is the fee amount thats it.

so by choosing a student, by knowing his class we can identify the fee details

Here term1 is a fee name which is paid in the month of june (date 1 to 15 thats it)

Term2 is a fee name which is paid september (date 1 to 15)

Term 3 is a fee name which is paid December

Just a name for the fee to be collected from the student. whether abdul from V std A section had paid the term1 fee or not thats it.

Kindly dont hesitate and reply please

drewdan's avatar

As @snapey said, try rewriting it using Eloquent, looking at all that and I cannot understand whats going on.

If you use Eloquent you could apply scopes such as paid and not paid, and then use methods like sum() to get the total amounts.

In addition to that, blade files (in my opinion) should be used to output data, your calculations should be elsewhere

AbdulBazith's avatar

@snapey @drewdan i think i confused you guys sorry. you may confused where iam checking whether paid or not.

actually i will fetch the term fee details for specific class from term fee details, and details of that specific class from fee payment table and then i will compare the amounts in the blade file.

say for example if i get term fee 2000 for V std from fee table. if Abdul from V std paid 1000 rupees and that will be recorded in feepayment table.

so in the blade file i will compare the class and student_id with that term fee. so 2000-1000. so Abdull need to pay 1000 rupees which is a balance.

if Abdul paid 2000 rupess then i will compare it so the difference will be 0. so i will change it as paid.

this is what iam doing.

Now only one doubt.

i have three tables

TermFeeTable StudentTable TermFeePaymentTable All the three tables have class_id column.

now my doubt is i need to get records from all the three tables.

here one thing.


$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();


here TermFeeTable with model name TermfeepaymentFromstudent iam fetching records from both table.

i need to fetch record from studenttable also how??

simply to say

from studenttable fetch record to that specific class, and the go to TermFee table fetch record for specific class and the sub model for TermFee that is TermfeepaymentFromstudent fetch it

how this is possible???

in a single query

$query= StudentTable ->
		
		With[termFeetable->With[TermfeepaymentFromstudent]] 

// studenttable with termfeetable and insde it i need to put a with termfeepaymentfromstuent.


my model

//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 think you undertstood guys

drewdan's avatar

We mean, write your query using Eloquent, then you can use relations to fetch from other tables and build queries upon it

Please or to participate in this conversation.