I'm making a Laravel 5.4 application, but I'm having a hard time figuring out how I should structure my data with eloquent relationships .
This is my models and how I want them to be related:
School → Has classes, users and events
User → Can belong to a school. Can have classes and sessions (with cases)
Class → Belongs to a school. Has users and subjects. Can have homework
Subject → Belongs to a class
Session → Belongs to a user. Can have cases
Case → Belongs to a session
Event → Belongs to a school
Homework → Belongs to a class
How should I structure this with eloquent relation functions (belongsTo, hasMany and so on) in my Laravel 5.4 project?
Assuming Class, User and Event models has a property school_id and the primary key you ant to use is id of the respective model, your Class, User, Event and School models should look like as follow.
School
class School extends Model
{
public function users(){
return $this->hasMany('App\User');
}
public function classes(){
return $this->hasMany('App\Class');
}
public function sessions(){
return $this->hasMany('App\Session');
}
}
User
class User extends Model
{
public function school(){
return $this->belongsTo('App\School');
}
public function classes(){
return $this->hasMany('App\Class');
}
public function events(){
return $this->hasMany('App\Event');
}
}
Class
class Class extends Model
{
public function school(){
return $this->belongsTo('App\School');
}
public function users(){
return $this->hasMany('App\User');
}
public function subjects(){
return $this->hasMany('App\Subject');
}
public function homeworks(){
return $this->hasMany('App\Homework');
}
}
Event
class Class extends Model
{
public function school(){
return $this->belongsTo('App\School');
}
}
You can use these relationships to define queries with chaining capability. eg if you want to get all the events associated with a School that has a id property equals to $id you can write,
$events = App\School::find($id)->events;
Laravel Documentation explains it well
The correct way to do this is
SCHOOL
public function classes()
{
return $this->hasMany('App\Class');
}
public function users()
{
return $this->hasMany('App\User');
}
public function events()
{
return $this->hasMany('App\Event');
}
CLASS
public function school()
{
return $this->belongsTo('App\School');
}
public function subjects()
{
return $this->hasMany('App\Subject');
}
public function homeworks()
{
return $this->hasMany('App\Homework');
}
public function users()
{
return $this->belongsToMany('App\User','class_users','class_id','user_id');
// this should be many to many because users can also have many classes
}
USER
public function school()
{
return $this->belongsTo('App\School');
}
public function classes()
{
return $this->belongsToMany('App\Class','class_users','user_id','class_id');
// this should be many to many as explained to class
}
public function sessions()
{
return $this->belongsToMany('App\Session','session_users','user_id','session_id');
// like classes do, this should be many to many relationship because sessions can also have many users
}
SUBJECT
public function class()
{
return $this->belongsTo('App\Class');
}
SESSION
public function users()
{
return $this->belongsToMany('App\User','session_users','session_id','user_id');
// should be many to many as well
}
public function cases()
{
return $this->hasMany('App\Case');
}
CASE
public function session()
{
return $this->belongsTo('App\Session');
}
EVENT
public function school()
{
return $this->belongsTo('App\School');
}
HOMEWORK
public function class()
{
return $this->belongsTo('App\Class');
}
With the School model and underlying table created, it's time to create the relation. Open the School model and create a public method named classes, users and events; inside it referencing the hasMany method:
School :
class School extends Model {
public function classes()
{
return $this->hasMany('App\Class');
}
public function users()
{
return $this->hasMany('App\User');
}
public function events()
{
return $this->hasMany('App\Event');
}
}
User :
class User extends Model {
public function school(){
return $this->belongsTo('App\School');
}
public function classes(){
return $this->hasMany('App\Class');
}
public function sessions(){
return $this->hasMany('App\Session');
}
}
Class :
class Class extends Model {
public function school(){
return $this->belongsTo('App\School');
}
public function users(){
return $this->hasMany('App\User');
}
public function subjects(){
return $this->hasMany('App\Subject');
}
public function homeworks(){
return $this->hasMany('App\Homework');
}
}
Subject :
class Subject extends Model {
public function class(){
return $this->belongsTo('App\Class');
}
}
Session:
class Session extends Model {
public function user(){
return $this->belongsTo('App\User');
}
public function cases(){
return $this->hasMany('App\Case');
}
}
Case :
class Case extends Model {
public function session(){
return $this->belongsTo('App\Session');
}
}
Event :
class Event extends Model {
public function school(){
return $this->belongsTo('App\School');
}
}
Homework:
class Homework extends Model {
public function class(){
return $this->belongsTo('App\Class');
}
}
For more details of hasMany relationship, Please check the link here : EasyLaravelBook
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.