0
votes

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?

3
How would you do it without eloquent? Have you tried to write down the database structure?ka_lin
You already know the relationships. It should be easy to do it in Laravel if you read the Relationships documentation on Laravel.Mozammil
The main problem is that I don't know when I should use the "hasManyThrough" relation. Could someone explain when to use this?Kris D. J.
@KristofferDamborgJørgensen Probably not. For two reasons: (1) It is not what you ask in your question and (2) it is a broad question. Plus, you should keep it stupid simple, go with the other relationships if you understand them more, it should do the trick for what you asked.Wistar
@Wistar - Makes a lot of sense to keep it stupid simple :D How about foreign keys (like user_id etc.), do I need to add them myself, or does eloquent do that for me? :)Kris D. J.

3 Answers

1
votes

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. e.g. 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

1
votes

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');
}
-1
votes

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