gpt4 book ai didi

laravel - 获取具有 Eloquent 关系的 parent 数据

转载 作者:行者123 更新时间:2023-12-02 16:56:22 26 4
gpt4 key购买 nike

我有这样的表:

主表

    id      titile      desc
1 one one is one
2 two two is two

子表

    id      value       master_id
1 first 1
2 second 1
3 third 1
4 fourth 1

所以当我成功检索数据时,它将是这样的 json:

    Master_Table {
id:1,
title:'one',
desc: 'one is one',
Child_Table: [
{id: 1, value:"first", master_id:1},
{id: 2, value:"second", master_id:1},
{id: 3, value:"third", master_id:1},
{id: 4, value:"fourth", master_id:1},
]
}

我这样做:

    $json= PDH::find(1)->products()->where('product_highlight_id', 1)->get();

但结果只是 child

        Child_Table: [
{id: 1, value:"first", master_id:1},
{id: 2, value:"second", master_id:1},
{id: 3, value:"third", master_id:1},
{id: 4, value:"fourth", master_id:1},
]

我确信 Laravel Eloquent 中可以使用此功能,但我很难找到它。有人可以向我指出吗?

提前致谢。

最佳答案

使用with()方法:

$pdh = PDH::with('products')->where('product_highlight_id', 1)->find(1);

然后 $pdh 将包含父对象,而 $pdh->products 将是产品集合。

关于laravel - 获取具有 Eloquent 关系的 parent 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48098739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com