0

Having some troubles with relations in Laravel not sure is there any solutions but hope you guys help me. So let's suppose I have 2 tables USERS and TRAINEES. USERS can be considered as company, admin, instructors depends on field role. About relations USER as company can have many TRAINEES but also as instructors can have many TRAINEES. So in this case how can I build relations between them? table TRAINEES should have fields COMPANY_ID and INSTRUCTOR_ID ? or how? But it's same table USERS. I don't know if it's clear for you. Just ask me. Count on you guys

Marat
  • 59
  • 8
  • I wouldn't differentiate in the second table, rely on the data in the user table `depends on field role` the reason is you will still need to know that to join on two copies of the same foreign key (if I understand what you are saying) and you would have to maintain it. Instead simply join on the user table and check the role. Instead of `COMPANY_ID and INSTRUCTOR_ID` use `USER_ID` and `ROLE` – ArtisticPhoenix Oct 30 '18 at 18:35
  • https://stackoverflow.com/questions/17567305/laravel-many-to-many-self-referencing-table-only-works-one-way may point you in the right direction. – ceejayoz Oct 30 '18 at 18:35
  • I typically go for `role, role_user, user` this way a trainee can be a user and a user can be a trainee if necessary. – Adam Rodriguez Oct 30 '18 at 18:44

1 Answers1

0

Design the users and trainees tables as follows:

users table columns/fields:

  • id (primary key)
  • username (string)
  • role (string, index)

trainees table columns/fields:

  • id (primary key)
  • name (string)
  • trainer_id (integer, index)
  • company_id (integer, index)

Then in your Trainees model, do this:

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

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

In your User model, do this:

public function trainees()
    {
        if($this->attributes["role"] == "company")
            return $this->companyTrainees();
        if($this->attributes["role"] == "instructor")
            return $this->instructorTrainees();
        return array();
    }

public function companyTrainees()
    {
        return $this->hasMany('App\Trainee', 'trainer_id');
    }

public function instructorTrainees()
    {
        return $this->hasMany('App\Trainee', 'company_id');
    }

Use these relationships in your controllers and other places. This is not tested, but it should not be too buggy. Don't forget to seed the database with valid data, especially for the ids.

Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29
  • Thanks a lot. I like ur solution, u gave me right direction i think. It needs little changes and will be perfect. Anyway thanks – Marat Oct 31 '18 at 17:36
  • Please spell out the changes so I update the answer, it may be useful to others – Elisha Senoo Oct 31 '18 at 18:46
  • I think here's all perfect except method trainees in User model. I didn't write a code yet but I think in my case there will no "if" conditions) – Marat Nov 01 '18 at 18:43