请问远程一对多,可以获取子层下面的所有模型嘛?

请教一下各位大神

A 一对多 B
B 一对多 C

如何通过 A,既能取到 B,也能在 B 中取到 C?

class Questionnaire extends Model
{
    public function titles()
    {
        return $this->hasMany(QuestionnaireTitle::class);
    }
}

class QuestionnaireTitle extends Model
{
    public function questions(): HasMany
    {
        return $this->hasMany(QuestionnaireQuestion::class);
    }
}

class QuestionnaireQuestion extends Model
{
    public function title()
    {
        return $this->belongsTo(QuestionnaireTitle::class);
    }
}

使用方式和结果

$questions = Questionnaire::where('id', $questionnaire_id)
            ->with('titles.questions')
            ->get()

$questions => Illuminate\Database\Eloquent\Collection {#3652
     all: [
       App\Models\Questionnaire {#3651
         id: 3,
         titles: Illuminate\Database\Eloquent\Collection {#3653
           all: [
             App\Models\QuestionnaireTitle {#3658
               questionnaire_id: 3
               questions: Illuminate\Database\Eloquent\Collection {#3655
               //无法获取到此处的值
                 all: [],
               },
             },
           ],
         },
       },
     ],
   }
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

我这没问题哎

class First extends Model
{
    use HasFactory;

    public function seconds(){
        return $this->hasMany(Second::class, 'first_id', 'id');
    }
}

class Second extends Model
{
    use HasFactory;

    public function thirds(){
        return $this->hasMany(Third::class);
    }
}

class Third extends Model
{
    use HasFactory;
}
>>> \App\Models\First::query()->with('seconds.thirds')->get()
=> Illuminate\Database\Eloquent\Collection {#4476
     all: [
       App\Models\First {#4475
         id: 1,
         test: "111",
         created_at: null,
         updated_at: null,
         seconds: Illuminate\Database\Eloquent\Collection {#4478
           all: [
             App\Models\Second {#4483
               id: 1,
               first_id: 1,
               test_s: "2222",
               created_at: null,
               updated_at: null,
               thirds: Illuminate\Database\Eloquent\Collection {#4490
                 all: [
                   App\Models\Third {#4493
                     id: 1,
                     second_id: 1,
                     text_t: "2_222",
                     created_at: null,
                     updated_at: null,
                   },
                 ],
               },
             },
             App\Models\Second {#4485
               id: 2,
               first_id: 1,
               test_s: "333",
               created_at: null,
               updated_at: null,
               thirds: Illuminate\Database\Eloquent\Collection {#4482
                 all: [
                   App\Models\Third {#4495
                     id: 2,
                     second_id: 2,
                     text_t: "2+_222",
                     created_at: null,
                     updated_at: null,
                   },
                 ],
               },
             },
           ],
         },
       },
     ],
   }

>>> 
1年前 评论
Partrick (楼主) 1年前
Partrick (楼主) 1年前
讨论数量: 12

你先单独用下titles那个模型看能不能获取到questions这个关联

1年前 评论
Partrick (楼主) 1年前
lun1bz (作者) 1年前
Partrick (楼主) 1年前
lun1bz (作者) 1年前
Partrick (楼主) 1年前
lun1bz (作者) 1年前

我猜测是不能的

假如可以的话:A 调用 B,B 调用 C, C 调用 A, 那不就嵌套了吗?

1年前 评论
Partrick (楼主) 1年前

我这没问题哎

class First extends Model
{
    use HasFactory;

    public function seconds(){
        return $this->hasMany(Second::class, 'first_id', 'id');
    }
}

class Second extends Model
{
    use HasFactory;

    public function thirds(){
        return $this->hasMany(Third::class);
    }
}

class Third extends Model
{
    use HasFactory;
}
>>> \App\Models\First::query()->with('seconds.thirds')->get()
=> Illuminate\Database\Eloquent\Collection {#4476
     all: [
       App\Models\First {#4475
         id: 1,
         test: "111",
         created_at: null,
         updated_at: null,
         seconds: Illuminate\Database\Eloquent\Collection {#4478
           all: [
             App\Models\Second {#4483
               id: 1,
               first_id: 1,
               test_s: "2222",
               created_at: null,
               updated_at: null,
               thirds: Illuminate\Database\Eloquent\Collection {#4490
                 all: [
                   App\Models\Third {#4493
                     id: 1,
                     second_id: 1,
                     text_t: "2_222",
                     created_at: null,
                     updated_at: null,
                   },
                 ],
               },
             },
             App\Models\Second {#4485
               id: 2,
               first_id: 1,
               test_s: "333",
               created_at: null,
               updated_at: null,
               thirds: Illuminate\Database\Eloquent\Collection {#4482
                 all: [
                   App\Models\Third {#4495
                     id: 2,
                     second_id: 2,
                     text_t: "2+_222",
                     created_at: null,
                     updated_at: null,
                   },
                 ],
               },
             },
           ],
         },
       },
     ],
   }

>>> 
1年前 评论
Partrick (楼主) 1年前
Partrick (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!