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
Php 拉拉维尔的hasMany和Belongtomany之间的关系_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 拉拉维尔的hasMany和Belongtomany之间的关系

Php 拉拉维尔的hasMany和Belongtomany之间的关系,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有两个模型——InternalNotesThread和InternalNotes class InternalNotesThread extends Model { public function notes(){ return $this->hasMany('App\InternalNotes', 'thread_id', 'id'); } } class InternalNotes extends Model { public function thr

我有两个模型——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映射到具有belongToMany关系的用户

数据库结构:

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
对于每个注释,可能都有一些用户标记在其中

我如何直接将这种关系与内部线程联系起来

我通过以下方式获得数据:

$data = InternalNotesThread::with('notes')->where('ticket_id', '=', $id)->get()->toArray();
但是,在notes数组中,我无法在该注释中标记用户


如何一次性获取所有数据?

您并不急于加载用户,因此他们不会出现在->toArray数组中

试试这个:

$data = InternalNotesThread::with('notes.users')
    ->where('ticket_id', '=', $id)
    ->get()
    ->toArray();

对不起,输入错误。编辑。工作起来很有魅力!谢谢如果我只想从user表中获取user\u id怎么办。现在它向我显示了所有字段。请参见->类似于带有'notes.users'=>函数$query{$query->select['id'];}是的,我知道这一点。但是,在这种情况下,它不起作用。发布一个关于这个问题的新问题和一些细节,我会看看我是否能提供帮助。