【问题标题】:Laravel - filter result from another sql tableLaravel - 从另一个 sql 表中过滤结果
【发布时间】:2020-12-02 02:41:53
【问题描述】:

现在我有抓取事件的功能(它也会给出参与者 ID),但我有另一个表 带有参与者 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;

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

【问题讨论】:

    标签: laravel laravel-5 query-builder laravel-query-builder


    【解决方案1】:

    我猜你可能需要在with() 子句上添加一个闭包过滤器

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      相关资源
      最近更新 更多