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

AbdulBazith's avatar

Problem many to many relationship with class and fees

Guys iam working with a project school management system.

I have to work with feePayment module.

The fee payment structure is left to the client to make their won structure of fee.

In all schools the fee structure would be like category wise. 1 main category and for that main category there will sub categories

how the process is done is

the school posses Main category and sub category of fee, like below structure


Term1     payment duration (1-08-2019 to 15-08-2019)
    school Fee =  6000
    Bus Fee   =  3000
    Tution Fee= 2500

Term 2  payment duration (1-12-2019 to 15-12-2019)
    school Fee = 6000
    Lab Fee   =   3000

Term3   payment duration (1-03-2020 to 15-08-2020)
    School Fee= 6000
    extra fee = 2000

like this it goes on,

here Term1, Term2 Term3 are main cateogries and school fee, extra fee tution fee are sub categories

this is my table strucutre : https://imgur.com/KxLpQWI

Whats my problem is

the below given are three tables. how can i link them with many to many or how kindly give some suggestion please. how to link these three tables

Feemaincategory         Feesub category         class

how this works is,

``` 5th std   -Term1  -> (tutionfee, bus fee) ```

``` 6th std -  Term1-> (tution fee, bus fee) ```

here class, fee main category, fee sub category are included. how i can insert these?? how i need to make the db kindly someone suggest please

0 likes
5 replies
AbdulBazith's avatar

I have drawn the table in my note: https://imgur.com/rmJWDSK

Kindly refer this and suggest your idea guys please.. how to relate these three tables.

a single class can have multiple main category and a single main category can have multiple class and a a single main category can have multiple sub category

AbdulBazith's avatar

i have drawn the unstructured table also: https://imgur.com/DD1CHud

This is my unstructured table format. here class, fee main category, fee sub category amount all included. how i need to split it.

Kindly some one suugest please

mstrauss's avatar

@abdulbazith

If I am understanding the problem correctly, then the below should do the trick. Note, I changed your Class model to Course as class is a reserved keyword in PHP.

Course Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    /**
     * Get the fee main categories associated with this Course
     */
    public function feeMainCategories()
    {
        return $this->hasMany('App\FeeMainCategory');
    }
}

FeeMainCategory Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class FeeMainCategory extends Model
{
    /**
     * Get course that this  fee main categories belongs to
     */
    public function course()
    {
        return $this->belongsTo('App\Course');
    }
}

 /**
     * Get the fee sub categories associated with this fee main category
     */
    public function feeSubCategories()
    {
        return $this->hasMany('App\FeeSubCategory');
    }
}

FeeSubCategory Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class FeeSubCategory extends Model
{
    /**
     * Get fee main category that this  fee sub categories belongs to
     */
    public function feeMainCategory()
    {
        return $this->belongsTo('App\FeeMainCategory');
    }
}

Please or to participate in this conversation.