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

使用Sum函数调用未定义方法的Laravel,laravel,eloquent,eloquent-relationship,Laravel,Eloquent,Eloquent Relationship,Laravel 8及其最新更新 客户有许多机器,机器有许多帐户记录 这项工作{{machines.accounts}} Route::get('/', function () { return Customer::with([ 'machines', 'machines.accounts', 'user' ]) ->get(); }); 但是with sum()或with count()不起作用 Route:

Laravel 8及其最新更新

客户有许多机器,机器有许多帐户记录

这项工作{{machines.accounts}}

Route::get('/', function () {
    return Customer::with([
        'machines',
        'machines.accounts',
        'user'
    ])
        ->get();
});
但是with sum()或with count()不起作用

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

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

使用with('machines',callback)和inside add withSum()

不使用2次
with
with
只使用1次
with
with sum
2次with work,但当它与这样简单的关系时('accounts','credit')>我认为您需要使用callback,然后调用未定义的方法App\Models\Customer::machines.accounts()。是的!这是工作,感谢Kamlesh抽出时间。祝您一切顺利。