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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器