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

hfalucas's avatar

Record Ownership (with multiple owners types)

Let me explain that better with a practical example. Imagine you have a typical User model and there's two types of users (Professors & Students). Then another model can be Book and in this table there are the following fields: id, professor_id,student_id etc..

In the User model the relationships are:

  public function books() 
  {
    return $this->hasMany('App\Book', 'student_id');
  }

Is that correct? Can I do the same for professor_id?

And to finish this how can I check the resource ownership, so only students can see their books and the professors can see, edit etc their books as well?

Can anyone point me in some direction? Any tutorial, video, book?

As you can see I'm kinda lost here :)

PS: Both users can login ofc.

0 likes
13 replies
graham's avatar

Is it necessary to have 2 separate models for your users? It might be easier to have 1 user table with a usertype field which you could set to either student or professor.

hfalucas's avatar

Hi @Graham there's only one user model. Imagine that I login as Professor, create a book for the a student with and id of 2.

Then the books table will have a record of: id: 1, professor_id: , student_id: 2, ....

So when later the student logs in can see his books, as well as the professor see his when logged in. (Hope this make sense)

graham's avatar

If a professor creates a book, does that book belong to a single student or can that book belong to many students?

graham's avatar

@hfalucas and does a student only have 1 book or can a student have multiple books?

hfalucas's avatar

@Graham Sorry should also had mentioned that. A student can have multiple books.

JarekTkaczyk's avatar
Level 53

@hfalucas @Graham There is no place for m-m relationship here. It's simple hasMany. There's only one thing that you didn't describe - how do you differentiate Professor from Student if they use the same model?

Anyway, that's not important in this case, because all you need is this (assuming you want to track who created the book author_id and who is the book assigned to owner_id - such names are more readable for the sake of example, you may use student_id and professor_id):

// User model
public function booksCreated()
{
    return $this->hasMany(Book::class, 'author_id');
}

public function booksOwned()
{
    return $this->hasMany(Book::class, 'owner_id');
}

// Book model
public function author()
{
    return $this->belongsTo(User::class, 'author_id');
}

public function owner()
{
    return $this->belongsTo(User::class, 'owner_id');
}

And you use the relationship you need depending on the user type, that's all.

1 like
hfalucas's avatar

@JarekTkaczyk Exactly.

Btw how would you differentiate the Professor and Student using the same model is that possible? Is there a need for roles?

I was thinking in create a Professor and Student model and make them extend the User model, is there a better way?

JarekTkaczyk's avatar

@hfalucas It depends on your app. The right way is to use separate models and so on and so forth. It may, but not necessarily will, be better in long run, but will be harder to implement (again, it's the right way, SOLID principles etc).

The easy way is to simply add a flag determining that a user is professor or student - if you don't expect more roles to come and there are not many reasons to separate them. This is the way for little app, when you don't really see the need for good design, but rather to make it work quickly.

hfalucas's avatar

I'm using roles already so will keep them. Just need to create the two extra models and make them extend the User model.

Never done that tho. The Login is still made using the User model right?

Once again thanks for the excellent answers @JarekTkaczyk :)

Please or to participate in this conversation.