Laravel 拉威尔有很多钥匙

Laravel 拉威尔有很多钥匙,laravel,has-many-through,Laravel,Has Many Through,我有3个模型:用户,程序,用户程序。UserProgram是它自己的实际模型 以下是模型在数据库中的外观: 使用者 身份证 节目 身份证 用户程序 用户id 程序id 我希望在我的计划模型中: function users() { return $this->hasManyThrough('App\User','App\UserProgram'); } 但这是行不通的。我如何才能使这种关系起作用?hasManyThrough不用于此目的。您需要一个多对多关系 cl

我有3个模型:用户,程序,用户程序。UserProgram是它自己的实际模型

以下是模型在数据库中的外观:

  • 使用者
    • 身份证
  • 节目
    • 身份证
  • 用户程序
    • 用户id
    • 程序id
我希望在我的计划模型中:

function users() {
    return $this->hasManyThrough('App\User','App\UserProgram');
}

但这是行不通的。我如何才能使这种关系起作用?

hasManyThrough
不用于此目的。您需要一个
多对多
关系

class Users {
  public function programs() {
    return $this->belongsToMany('App\Program', 'user_programs', 'user_id', 'program_id');
  }
}


非常感谢你!这很有帮助!
class Program {
  public function users() {
    return return $this->belongsToMany('App\User', 'user_programs', 'program_id', 'user_id');
  }
}