Laravel-过滤来自另一个sql表的结果

问题描述

现在我具有捕获事件的功能(它也会提供参与者ID),但是我还有另一个表 参与者编号为participator_id的我,我需要按状态对其进行过滤

    public function eventSearch(Request $request)
    {
        return ParticipantEvent::with('participant')
        ->where('end_date','>=',Carbon::Now())
        ->get()
        ->map(function($event) {
        $event->url = route('public::participant::show',['participant' => $event->participant]);
        $participant = collect($event->participant->toArray())->all();  
            return $event;
        });
    }
    

我有解决方案来吸引参加者,状态为:

    $participants = Participant::where(
        function ($query) {
            $query->whereHas(
                'participantElections',function ($subQuery) {
                    $subQuery->where('status','=','good');
                }
            )
        }
    )->get();
    return $participants;

但是如何结合这两种解决方案?

解决方法

我想您可能需要在with()子句上添加一个闭合过滤器

ParticipantEvent::with([
'participant' => function ($query) {
            $query->whereHas(
                'participantElections',function ($subQuery) {
                    $subQuery->where('status','=','good');
                }
            )
        }
    ])
    ->where('end_date','>=',Carbon::now())
    ->get()