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

mightyteja's avatar

Display the Designation role corresponding to the member - Joining 3 tables

I am trying to display the Designation role corresponding to the member,

What I am trying to achieve,

Chairman |
         |
         President - Heading
          - member name
         Vice President - Heading
          - member name

I have a chairman with subordinates who are divided based on designation ID,

To achieve this, I have created 3 table,

statechairmen

id | first_name | from | to  

1 | Chairman | 2019 | 2020

statemembers

id | first_name | chairman_id | designation_id 

  1 | Member 1 | 1 | P (inherted from designation_code of statedesignation)

  1 | Member 2 | 1 | VP (inherted from designation_code of statedesignation)

statedesignation


id | designation_code | designation_name | designation_desc

  1 | P | President | President Description. 
  1 | VP | Vice President | President Description. 

Can the database design be better ?? . Any other suggestions

I would like to display the list of members followed by their designation:

I tried ordering the table but, I am not able to bring in the designation for the corresponding members,

My controller to bring in the Statemembers under the chairman

$statemembers = StateChairman::find($id)->statemembers()->orderBy('designation_id')->get();
dd($statemembers);

I am getting result- List of all state members -

But when I try this

$statedesignation = Statemembers::find($id)->statedesignations()->get();
dd($statedesignation);

For this one I am only getting result with Id 1 in the Statedesignation, that is President, The Vice President is ignored,

I am only getting Designation President since find($id) brings up only 1 or corresponding id

Model:

State Chairman:

class Statechairman extends Model
{
  public function statemembers(){
      return $this->hasMany('App\Statemembers','chairman_id','id');
  }
}

StateMembers:

class Statemembers extends Model
{
    public function statechairman(){
        return $this->belongsTo('App\Statechairman','chairman_id','id');
    }

State Designations:

 public function statemembers(){
    return $this->hasMany('App\Statemembers');
}
0 likes
0 replies

Please or to participate in this conversation.