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

Frames's avatar

Relation between same Users table

I'm mindf*cking myself trying to make a relationship between users on same 'Users' table.

I've got 3 different roles (Admin, Coach and Runner). A user has only one role.

The issue is when I try to relate a user with role Coach and user with role Runner. Logic app is this one; I can create runners and coaches but each runner belongs only to one coach and one coach has many runners.

How can I handle this on same User table and then on User model.

When I solve this, I need to associate one coach and one runner to trainings table, but this is another brainkiller.

0 likes
5 replies
audunru's avatar

The key (pun intended) to solve this is to specify the coach_id as the column name when you setup the relationships. You need to add that column in the database.

class User extends Authenticatable
{
    public function coach()
    {
        return $this->belongsTo('App\User', 'coach_id');
    }

    public function runners()
    {
        return $this->hasMany('App\User', 'coach_id');
    }
}
EslamAhmed's avatar

I think it's better if you isolate this extra data because it's a data related only to runners user roles, so you should create a new table 'runners' and this table will have:

user_id: the user id of the runner

coach_id: the user id of the coach

In that situation, there will be a Runner model that will have a one-to-many relationship with the users table through the "coach_id" column

And if the coaches have more and different data than the runners (for example years of experience or his gym's address) then you should create a new table for coaches as well. Then the models and relations will change based on that.

Check the documentation here for more info about relations and how to do it in laravel models https://laravel.com/docs/5.8/eloquent-relationships

________________

Generally, if a specific user role has some data that the other roles don't have it, you shouldn't put it in the users table

jlrdw's avatar

You probably need an additional field in runner table to relate Coach and Runner via something like coach_id.

That way any runner, you will know who the coach is. Some folks use a pivot table for this.

DavidPetrov's avatar

Provided that you have a coach_id field on your users table, this must do the job.

User model:

//when a runner
public function coach()
{
    return $this->belongsTo('App\User', 'coach_id');
}

//when a coach
public function runners()
{
    return $this->hasMany('App\User', 'coach_id');
}
RomainB's avatar

https://laravel.com/docs/5.8/eloquent-relationships#one-to-many

In Laravel Documentation, it says:

Remember, Eloquent will automatically determine the proper foreign key column on the Comment model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with _id. So, for this example, Eloquent will assume the foreign key on the Comment model is post_id.

Like the hasOne method, you may also override the foreign and local keys by passing additional arguments to the hasMany method:

return $this->hasMany('App\Comment', 'foreign_key');

In your case it's the same table, so Laravel is searching a "user_id" inside the user table > There is not.

Add a coach_id column which you fill when a coach is selected(nullable if runner doesn't have one) and specify the coach user id inside of it. Then declare it in your belongsTo and hasMany methods:

class User extends Authenticatable
{
    public function coach() // Return Coach
    {
        return $this->belongsTo('App\User', 'coach_id');
    }

    public function runners() // Return User
    {
        return $this->hasMany('App\User', 'coach_id');
    }
}

Please or to participate in this conversation.