dongxuan1660 2015-01-19 10:56 采纳率: 100%
浏览 123
已采纳

Laravel过滤'with'结果

I'm making a web app which includes a query like the below:

$entry = Entry::with([
    'elements',
    'competition.groups.fields',
    'competition.groups.reviews'
])->find($id);

What I'd like to do is add something along the lines of:

$entry = Entry::with([
    'elements',
    'competition.groups.fields',
    'competition.groups.reviews' -> where('user_id', '=', Auth::user()->id)
])->find($id);

So I'd like to grab the reviews which belong to the entry, where the review's user_id also matches the currently logged in user. Is there a way of doing this?

  • 写回答

2条回答 默认 最新

  • douyi1779 2015-01-19 11:58
    关注

    You can just add a closure for filtering with results:

    $entry = Entry::with([
        'elements',
        'competition.groups.fields',
        'competition.groups.reviews' => function($q){
            $q->where('user_id', '=', Auth::id()); // Replaced Auth::user()->id with a shortcut: Auth::id()
        }
    ])->find($id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景