Php 拉威尔过滤';与';结果

Php 拉威尔过滤';与';结果,php,laravel,Php,Laravel,我正在制作一个包含如下查询的web应用程序: $entry = Entry::with([ 'elements', 'competition.groups.fields', 'competition.groups.reviews' ])->find($id); 我想做的是添加以下内容: $entry = Entry::with([ 'elements', 'competition.groups.fields', 'competition.g

我正在制作一个包含如下查询的web应用程序:

$entry = Entry::with([
    'elements',
    'competition.groups.fields',
    'competition.groups.reviews'
])->find($id);
我想做的是添加以下内容:

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

因此,我想获取属于条目的评论,其中评论的用户id也与当前登录的用户匹配。有没有办法做到这一点?

你也可以试试这段代码

$entry = Entry::select('elements', 'competition.groups.fields', 'competition.groups.reviews')->where('user_id', '=', Auth::user()->id)->get();
此代码将获取属于当前登录用户的
条目的集合


如果不存在条目
条目
采集计数将为零。如果存在某些条目,则您可以循环查看集合和抓取条目属性。

您只需添加一个闭包,用于过滤
结果:

$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);