duanhu7615 2016-12-01 05:58
浏览 39
已采纳

如何从laravel中的数据透视表中获取更多行

Hi I am trying to build an app where there are parent elements which have children and each child has its own sub child. I am having a pivot table which consists of user_id, plan_id and child_id, Now I am trying to fetch the child. How can I fetch all the children in an array.:

I am trying following code:

$user = Auth::user();
$selectplan = 1;
$children = $user->relations()->wherePlanId($selectplan)->first()->pivot->child;

I am able to get only first child, but I have more rows to be fetched:

/**** Table Column ****
*
*
/--id----user_id----plan_id----child----created_at----updated_at
    1        1         1         2        NULL          NULL
    1        1         2         3        NULL          NULL
    1        1         1         4        NULL          NULL
    1        2         1         7        NULL          NULL
    1        2         1         8        NULL          NULL
    1        2         3         10       NULL          NULL

Please help me out. Thanks!

  • 写回答

1条回答 默认 最新

  • duanlun1955 2016-12-01 07:11
    关注

    first only returns a single model instance; the first one matching the query constraints. In order to return a collection you need to use get

    $children = $user->relations()->wherePlanId($selectplan)->get()->pivot->child;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 请教一下simulink中S函数相关问题
    • ¥15 Hadoop中eclipse运行问题
    • ¥15 在二层网络中,掩码存在包含关系即可通信
    • ¥15 端口转发器解析失败不知道电脑设置了啥
    • ¥15 Latex算法流程图行号自定义
    • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
    • ¥40 基于51单片机实现球赛计分器功能
    • ¥15 cs2游戏画面卡住,应用程序sid与指挥者sid不匹配
    • ¥15 实验七:Pandas要有实验截图和代码
    • ¥15 TypeError: Make sure that the iterable only contains strings.