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

AbdulBazith's avatar

How to use child table as parent table for another table to fetch records.

Hi guys i stuck into a problem.

i have a small doubt in school management system project.

i have a manage form in that iam fetching all student class wise.

this is below code

$users = User::where('user_type', 'student')->where('status', 'active')->

            whereHas('admininfo', function ($query) use ($request) {

            if (!empty($request->class_id)) {

                $query->where('admin_class_id', $request->class_id);
            }
            if (!empty($request->section_id)) {

                $query->where('admin_section_id', $request->section_id);

            }
            if (!empty($request->student_id)) {

                $query->where('student_id', $request->student_id);

            }
        })->get();

        $users->load(['admininfo']);

the above code fetched all student in a class, and section. everything works fine.

my user table

id	student_name
1	A
2	B
3	C

my classDetails table

id	className
1	V std
2	Vi std

my admininfo table

id	student_id	class_id	section_id	status
1	1			1			1		active
2	2			1			1		active

here i have relation between user table with ->admininfo table

classDetails table has relation with admininfo table, fee structure table, fee payment table

user table has relation with fee payment table

i have a column fee paid or not. if the student paid the whole term fee it must show paid if he has any balance it must show pay

my fee structure table looks like this

id	class_id	fee_cat_name	fee_total_amt
1	1		Term1			1000
2	1		Term2			2000
3	1		Term3			3000
4	2		Term1			6000
.
.
.
Class_id  1 = V std

So for class V std Term1,2,3 Fee is = 6000

if student A paid 6000 he paid whole fees else he is pending

my fee payment table

id	class_id	student_id	section_id	Fee_cat_id	paid_amount
1	1		1			1			1			1000
2	1		1			1			2			2000
3	1		1			1			3			3000

so here student A that is id=1 he paid all 3 term fees 

Kindly some one help please.

and also i need to take total number of students in each class to calculate total fee.

i need two reports

first report 1: just displays all classes here

this is for term1 total student 6 in LKG so 6000
class	feeamount	totalamout	paidamt		balance
LKG		1000		6000		5000		1000
UKG
1STD
Vstd
.
.


  $feedetails = FeeMainCat::query()->where('fee_main_cat', $request->fee_cat_id)
        ->with([
            'feePaymentFromStudent' => function ($query)  {
                $query
                    ->selectRaw('fee_main_cat_id, SUM(paid_amt+disc_amt-fine_amt) as paid')->groupBy(['class_id', 'fee_main_cat_id']);
            },
        ])
        ->get();


// this is the query for this report. i can get the total student so only i can count the  totalamout means 1000*6=6000

Second Report i need to display all student class wise in that i need a colum paid or not as i said first

this is for class V std
studentname	fee_paid_or_not
A				paid
B				pending
C				pending
D				paid

in this above table i have wrote this coding and works fine. but it shows for each student only if click the student id the it shows


  $student = User::find($id);

        $student->load(['admininfo', 'personalinfo', 'addressinfo', 'parentsinfo', 'previousqualiinfo', 'siblingsinfo', 'feeconsinfo']);

     
        $fees = FeeMainCat::query()->where('class_id', $student->admininfo->admin_class_id)
            ->with([
                'feePaymentFromStudent' => function ($query) use ($id) {
                    $query
                        ->selectRaw('student_id, fee_main_cat_id, SUM(paid_amt) as paid')->selectRaw('student_id, fee_main_cat_id, SUM(disc_amt) as discamt')->
                        selectRaw('student_id, fee_main_cat_id, SUM(fine_amt) as fineamt')
                        ->
                    selectRaw('student_id, fee_main_cat_id, SUM(paid_amt+disc_amt-fine_amt) as checkamt')
                        ->where('student_id', $id)
                        ->groupBy(['student_id', 'fee_main_cat_id']);
                },
            ])
            ->get();

in the aboe code i pass a single student. but i need all the students of each class in single page

here for student A it goes and checks in fee structure table for V std total term fees how much and then it comes to feepayment table for that class that student how much he paid and it compares and gives paid or not.

i have given all tables and relationships above.

Kindly some one help pleasee

0 likes
0 replies

Please or to participate in this conversation.