如何在查询中写 table in Laravel 中的 where 条件

how to write where condition in relation table in Laravel in query

我有 3 个 table,即 ResortBookingExpense,这些 table 是关联关系。代码如下,

$resorts = Resort::where('status',1)->with('bookings')->withSum('bookings', 'amount')
        ->with('expenses')->withSum('expenses', 'amount')->get();

我想使用 date 字段对 table 进行排序。我如何在此查询中使用 wherebetween 进行预订和费用?

你可以像这样在 with() 中传递数组

$resorts = Resort::where('status', 1)
    ->with(
        ['bookings' => function ($q) {
            $q->wherebetween('colName', ['startDate','endDate']);
        }]
    )->withSum('bookings', 'amount')
    ->with(
        ['expenses' => function ($q) {
            $q->wherebetween('colName', ['startDate','endDate']);
        }]
    )->withSum('expenses', 'amount')
    ->get();

ref link https://laravel.com/docs/8.x/eloquent-relationships#constraining-eager-loads