【发布时间】:2015-07-29 03:01:47
【问题描述】:
我有两个模型 - InternalNotesThread 和 InternalNotes
class InternalNotesThread extends Model {
public function notes(){
return $this->hasMany('App\InternalNotes', 'thread_id', 'id');
}
}
class InternalNotes extends Model {
public function thread() {
return $this->belongsTo('App\InternalNotesThread');
}
public function users()
{
return $this->belongsToMany('App\User', 'user_notes_tagged', 'internal_notes_id', 'user_id');
}
}
此外,InternalNotes 映射到 User 的关系为 belongsToMany。
数据库结构:
internal_notes_thread:
id - int
ticket_id (FK) - int
name - string
internal_notes:
id - int
thread_id (FK) - int
notes - string
user_notes_tagged: (Many to many with internal_notes and User)
internal_notes_id (FK) - int
user_id (FK) - int
对于每条笔记,都可能有一些用户被标记在其中。
我怎样才能直接将这种关系与 internal_notes_thread 联系起来???
我用这个得到数据:
$data = InternalNotesThread::with('notes')->where('ticket_id', '=', $id)->get()->toArray();
但是,在 notes 数组中,我无法获取在该便笺中标记的用户
如何一次性获取所有数据???
【问题讨论】:
-
抱歉,拼写错误。已编辑。