Laravel 使用 Sum 函数调用未定义的方法

Laravel With Sum Function Call to undefined method

Laravel 8 最新更新

客户有很多机器,机器有很多账户记录

这项工作{{ machines.accounts }}

Route::get('/', function () {
    return Customer::with([
        'machines',
        'machines.accounts',
        'user'
    ])
        ->get();
});

但是 withSum() 或 withCount() 不起作用

Route::get('/', function () {
    return Customer::with([
        'machines',
        'user'
    ])
        ->withSum('machines.accounts', 'credit')
        ->get();
});

错误:调用未定义的方法App\Models\Customer::machines.accounts()

使用 with('machines', callback) 并在内部添加 withSum()