Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 拉雷维尔通过很多人进入了梦乡_Laravel_Laravel 4 - Fatal编程技术网

Laravel 拉雷维尔通过很多人进入了梦乡

Laravel 拉雷维尔通过很多人进入了梦乡,laravel,laravel-4,Laravel,Laravel 4,我有三种型号:会议,会议和演讲者。一个会议可以有许多会议,许多会议可以有许多发言者 数据库结构 我需要写一个方法,使我能够得到一个特定会议的所有发言者(因此,所有发言者从该特定会议的所有会议) 下面说明了我认为应该有效的方法,但显然不行,因为我无法将这些方法链接在一起 app/models/Conference.php 我已经设置了所有模型关系,并且工作正常(会议会话、会话演讲者),但是我无法在会议会话演讲者之间建立桥梁。有人知道我如何在不编写大型SQL连接查询的情况下实现这一点吗 我认为,如果

我有三种型号:
会议
会议
演讲者
。一个会议可以有许多会议,许多会议可以有许多发言者

数据库结构 我需要写一个方法,使我能够得到一个特定会议的所有发言者(因此,所有发言者从该特定会议的所有会议)

下面说明了我认为应该有效的方法,但显然不行,因为我无法将这些方法链接在一起

app/models/Conference.php 我已经设置了所有模型关系,并且工作正常(会议会话、会话演讲者),但是我无法在会议会话演讲者之间建立桥梁。有人知道我如何在不编写大型SQL连接查询的情况下实现这一点吗

我认为,如果有一个关系
属于tomanythrough()
,这将起作用,但实际上没有


提前谢谢

不幸的是,hasManyThrough关系不适用于两者之间的多对多关系

您可以这样做:

public function speakers() {
  $session_ids = Session::where('conference_id', $this->id);
  $speaker_ids = DB::table('session_speaker')->whereIn('session_id', $session_ids)->lists('speaker_id');
  return Speaker::whereIn('id', $speaker_ids)->get();
}
如果没有找到结果,您可能需要将变量设置为
array(0)
,否则where函数将抛出错误。 您也可以使用一种雄辩的唯一方法,但这可能会导致更多的数据库查询,而这一种应该可以只运行2个查询


然后,您可以使用例如
Conference::find(1)->speakers()

访问演讲者。Twitter上有一些人说这也不可能,但感谢您的回复-我认为这是最糟糕的情况!
class Conference extends Eloquent {

  public function speakers() {
    return $this->hasMany('Session')->belongsToMany('Speaker');
  }

}
public function speakers() {
  $session_ids = Session::where('conference_id', $this->id);
  $speaker_ids = DB::table('session_speaker')->whereIn('session_id', $session_ids)->lists('speaker_id');
  return Speaker::whereIn('id', $speaker_ids)->get();
}