【问题标题】:Laravel With Sum Function Call to undefined methodLaravel 与 Sum 函数调用未定义的方法
【发布时间】:2021-02-16 18:45:24
【问题描述】:

具有最新更新的 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()

【问题讨论】:

  • 不要使用 2 次 withwithSum 仅使用 1 次 withwithSum
  • 2 次工作,但是当它像 这样简单的关系时
  • 我认为你需要使用回调
  • 调用未定义的方法 App\Models\Customer::machines.accounts()
  • 是的!它的工作,谢谢你的 Kamlesh 的时间。一切顺利。

标签: laravel eloquent eloquent-relationship


【解决方案1】:

使用 with('machines', callback) 并在里面添加 withSum()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 2015-05-31
    • 2016-06-05
    • 2016-09-03
    • 2015-06-10
    相关资源
    最近更新 更多