kjanko
kjanko

Reputation: 592

Eloquent model inheritance hierarchy

I have a case where 2 eloquent models should inherit properties from a User model, but the User itself should not exist as a standalone instance. (Mentors and Students, both inherit from User class). So what I'm currently doing is:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

abstract class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Get the courses that the user has enrolled into
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */

    public function courses()
    {
        return $this->hasMany('App\Models\Course', 'user_course', 'user_id', 'course_id');
    }
}

class Student extends User
{
    protected $table = 'students';

    /**
     * Get the mentors that the user has hired
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */

    public function mentors()
    {
        return $this->hasMany('App\Models\User');
    }
}

class Mentor extends User
{
    protected $table = 'mentors';

    /**
     * Get a list of courses that a mentor is teaching
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */

    public function ownCourses()
    {
        return $this->hasMany('App\Models\Course', 'mentor_course', 'mentor_id', 'course_id');
    }
}

I am wondering whether this is the correct to do what I am trying to accomplish?

Upvotes: 2

Views: 2395

Answers (1)

dparoli
dparoli

Reputation: 9161

IMHO I will use polymorhic relation:

Use three tables: users, students and mentors; in the users table add two fields: userable_id (integer), userable_type (string).

User model

class class User extends Authenticatable
{
    public function userable()
    {
        return $this->morphTo();
    }

Student model:

class Student extends Model
{
    public function user()
    {
        return $this->morphOne('App\User', 'userable');
    }

Mentor model:

class Mentor extends Model
{
    public function user()
    {
        return $this->morphOne('App\User', 'userable');
    }

Now User::find($id)->userable return a Student or a Mentor object depending on the value of the userable_type

I leave the others relations to you, I hope this helps.

Upvotes: 2

Related Questions