雄辩的关系(Laravel 5.4)

时间:2017-05-03 12:11:07

标签: php laravel eloquent laravel-5.4 laravel-eloquent

我正在制作 Laravel 5.4 应用程序,但我很难弄清楚如何使用雄辩的关系来构建数据

这是我的模型以及我希望它们如何相关:

  • 学校→有课程,用户和活动

  • 用户→可以属于某所学校。可以有课程和会话(有案例)

  • 班级→属于学校。有用户和科目。可以做作业

  • 主题→属于班级

  • 会话→属于用户。可以有案例

  • 案例→属于某个会话

  • 活动→属于学校

  • 家庭作业→属于班级

我应该如何在Laravel 5.4项目中使用雄辩的关系函数(belongsTo,hasMany等)来构建它?

3 个答案:

答案 0 :(得分:1)

假设Class,User和Event模型具有属性school_id,并且您使用的主键是相应模型的id,则Class,User,Event和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');
}
}

用户

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

事件

class Class extends Model
{
public function school(){
    return $this->belongsTo('App\School');
}

}

您可以使用这些关系来定义具有链接功能的查询。例如如果你想获得与学校相关的所有活动,其中id属性等于$ id,你可以写,

$events = App\School::find($id)->events;

Laravel Documentation解释得很好

答案 1 :(得分:1)

正确的方法是

<强>学校

public function classes()
{
    return $this->hasMany('App\Class');
}

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

public function events()
{
    return $this->hasMany('App\Event');
}

<强>类

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
}

用户

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

答案 2 :(得分:-1)

创建School模型和基础表后,就可以创建关系了。打开School模型并创建一个名为classes,users和events的公共方法;在它内部引用hasMany方法:

学校:

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

}

用户:

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

}

主题:

class Subject extends Model {

    public function class(){
        return $this->belongsTo('App\Class');
    }

}

会话:

class Session extends Model {

    public function user(){
        return $this->belongsTo('App\User');
    }

    public function cases(){
        return $this->hasMany('App\Case');
    }

}

案例:

class Case extends Model {

    public function session(){
        return $this->belongsTo('App\Session');
    }

}

活动:

class Event extends Model {

    public function school(){
        return $this->belongsTo('App\School');
    }

}

作业:

class Homework extends Model {

    public function class(){
        return $this->belongsTo('App\Class');
    }

}

有关hasMany关系的更多详细信息,请查看此处的链接:EasyLaravelBook