Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Json 如何从即时加载中获取联接表值?_Json_Laravel 5.2_Eager Loading - Fatal编程技术网

Json 如何从即时加载中获取联接表值?

Json 如何从即时加载中获取联接表值?,json,laravel-5.2,eager-loading,Json,Laravel 5.2,Eager Loading,我正在通过Laracasts关于egar加载的视频工作,到目前为止效果很好 我原以为我很好地掌握了这些概念,但现在我被过去的做法(CodeIgniter)和这种非常棒的方式所困扰 我有三张桌子: cards #card_id, title, created_at, updated_at notes #note_id, name, created_at, updated_at card_notes #card_id, note_id 在card\u notes中有

我正在通过Laracasts关于egar加载的视频工作,到目前为止效果很好

我原以为我很好地掌握了这些概念,但现在我被过去的做法(CodeIgniter)和这种非常棒的方式所困扰

我有三张桌子:

cards        #card_id, title, created_at, updated_at
notes        #note_id, name, created_at, updated_at
card_notes   #card_id, note_id
card\u notes
中有FK字段:

card_id  #FK back to cards->card_id
note_id  #FK back to notes->note_id
我能够获得给定卡片的所有注释:

CardsController.php

public function show(Card $card)
{
    $card->load('notes');
}
public function notes()
{
    return $this->hasMany(CardNote::class);
}
Card.php

public function show(Card $card)
{
    $card->load('notes');
}
public function notes()
{
    return $this->hasMany(CardNote::class);
}
json

{
    id: 1,
    title: "First Card",
    created_at: null,
    updated_at: null,
    notes: [
        {
            card_id: 1,
            note_id: 1,
            created_at: null,
            updated_at: null
         },
         {
            card_id: 1,
            note_id: 2,
            created_at: null,
            updated_at: null
         }
     ]            
}
{
    id: 1,
    title: "First Card",
    created_at: null,
    updated_at: null,
    notes: [
        {
            card_id: 1,
            note_id: 1,
            name: "John Doe"
            created_at: null,
            updated_at: null
         },
         {
            card_id: 1,
            note_id: 2,
            name: "Jane Doe"
            created_at: null,
            updated_at: null
         }
     ]            
}
到目前为止,一切都很好! 现在,在我的
notes
表中,我有一个
name
列要显示。这就是我被困的地方。我能够访问我的查找表,但我还没有想出如何通过该表进入联接表。看起来我需要
hasManyThrough
但是。。。我脸都快掉下去了

return $this->hasManyThrough(Note::class, CardNote::class);
我需要获取给定
注释id
的所有注释记录,但我不确定如何深入到下一个级别。这就是我最终想要产生的结果:

json

{
    id: 1,
    title: "First Card",
    created_at: null,
    updated_at: null,
    notes: [
        {
            card_id: 1,
            note_id: 1,
            created_at: null,
            updated_at: null
         },
         {
            card_id: 1,
            note_id: 2,
            created_at: null,
            updated_at: null
         }
     ]            
}
{
    id: 1,
    title: "First Card",
    created_at: null,
    updated_at: null,
    notes: [
        {
            card_id: 1,
            note_id: 1,
            name: "John Doe"
            created_at: null,
            updated_at: null
         },
         {
            card_id: 1,
            note_id: 2,
            name: "Jane Doe"
            created_at: null,
            updated_at: null
         }
     ]            
}

谢谢你的建议

如果你继续滚动,结果会有所帮助。我知道我和hasManyThrough很接近,我只是错过了可选参数

return $this->hasManyThrough(Note::class, CardNote::class, 'card_id', 'note_id');