mercredi 27 juillet 2016

Multiple table inheritance in Laravel 5.1

I've read some comments about how to implement multiple table inheritance in Laravel, but I'm confusing.

In this thread (Table inheritance in Laravel), I noted that a table inheritance was implemented. However, Laravel 5.1 documentation explains about "Polymorphic Relations" (http://ift.tt/1RvSF2y).

This is my scenario: A "Person" can be a "Student", "Teacher" and/or "Candidate". The same person can be Student and Teacher at the same time, for example. This is a way to implement the models I think (I do not know if it is correct):

class Person extends Model {
    public function teacher(){
        return $this->hasOne(Teacher::class);
    }
    public function student(){
        return $this->hasOne(Student::class);
    }
    public function candidate(){
        return $this->hasOne(Candidate::class);
    } 
}

class Student extends Person {
    public function person(){
        return $this->belongsTo(Person::class);
    }
}

class Teacher extends Person {
    public function person(){
        return $this->belongsTo(Person::class);
    }
}

class Candidate extends Person {
    public function person(){
        return $this->belongsTo(Person::class);
    }
}

In the database, I would create the tables:

  • Person (id, name, phone)
  • Student (person_id, etc)
  • Teacher (person_id, etc)
  • Candidate (person_id, etc)

This way, I do not use Polymorphic Relations as Laravel 5.1 explains. Any suggestions about the best way to accomplish this?

Thank you very much.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire