AbdulBazith's avatar

how toretrive staff_designation from one child table to another child table with one parent table

guys iam working with a school project.

i need to insert staff details so what i did is

i had a form which creates an account for staff and then other details in other table

users table below

id
user_name

official_info table

id
staff_id (fk from users table)
designation

here users in parent table and official_ info is child table

now i have another problem

i have an attendance table where columns are

id
staff_id (pk from users table)
att_date

now whats my problem when i display the attendance i need to display the staff_designation also.

for name i did like this

$attendance->staff->user_name   (as it is directly connected to user table)

but designation is in official_info table which is a child table then how?

simply to say user is parent table and attendance and official_info are child table. how can i access the official_table info by attendance table??

Kindly some one help please

my user model

 public function staffofficial()
     {
         return $this->hasOne('App\StaffOfficialInfo', 'staff_id');
     }

  public function staffattendance()
     {
         return $this->hasmany('App\StaffAttendance', 'staff_id');
     }


my staff attendance model

  public function staff()
    {
        return $this->belongsTo('App\User', 'staff_id');
    }



my official model

 public function user()
    {
        return $this->belongsTo('App\User', 'staff_id');
    }



0 likes
2 replies
geowrgetudor's avatar

Assuming all the users have an official_info reference by default and you want to get all the users who have staffattendance

$usersWithAttendence = Users::with('staffofficial')->whereHas('staffattendance')->whith('staffattendance')->get();
AbdulBazith's avatar

@geowrge thank you for your response and sorry for my delay response.

is this the only solution. why iam asking this because. generally we may have many table like this with one parent table and more than child table. so when need to call from one child table to another is this only procedure there else is there any other method or query

Please or to participate in this conversation.