代码之家  ›  专栏  ›  技术社区  ›  Kevin Pimentel

在拉维尔斯雄辩,我如何获得记录与关系?

  •  2
  • Kevin Pimentel  · 技术社区  · 6 年前

    我正在尝试提取所有具有事件关系且事件日期为今天的记录。我有以下代码:

        EventStaffStation::with([
            'event' => function($query) {
                $query->where('event_date', Carbon::now()->format('d-m-Y'));
            }
        ])->where([
            'staff_id' => auth()->user()->id
        ])->has('event')->get()
    

    基本上,如果今天没有活动记录,我不想把它退回。我只对今天活动日的唱片感兴趣。

    感谢您的帮助。

    2 回复  |  直到 6 年前
        1
  •  6
  •   Marcin Nabiałek    6 年前

    您应该使用:

    EventStaffStation::whereHas(
        'event' , function($query) {
            $query->where('event_date', Carbon::now()->format('d-m-Y'));
        }
    )->where([
        'staff_id' => auth()->user()->id
    ])->get()
    

    has/whereHas 而不是 with . 当然在上面的查询中你也可以添加 如果你需要的话,这将是你想要的任何关系。

        2
  •  1
  •   Aaqib Khan    6 年前

    你应该两者兼用 whereHas with .

    哪里有 具有

    EventStaffStation::whereHas(
        'event' , function($query) {
            $query->where('event_date', Carbon::now()->format('d-m-Y'));
        }
    )->with('event')
    ->where([
        'staff_id' => auth()->user()->id
    ])->get();