【问题标题】:How to return multiple custom values in model Laravel如何在模型 Laravel 中返​​回多个自定义值
【发布时间】:2021-07-04 02:04:10
【问题描述】:

我正在尝试做的是一个附属计划,我希望当我调用一个范围时,它会返回我想要的所有值。将有一个计算平台、卖家和附属公司价值的逻辑。

我已经拥有的是:

public function scopeProfitPlatform($query) {
    return $query->sum('price')*0.6;
}

public function scopeProfitAffiliate($query) {
    return $query->sum('price')*0.05;
}

public function scopeProfitBuyer($query) {
    return $query->sum('price')*0.35;
}

这可行,但我想让它更简单。因此,我不想调用 Order::profitplatform() and order::profitaffiliate, etc....,而是想调用 Order::profit(),它会一次性将所有这些值返回给我。

我尝试使用 Order::profitplatform()->profitaffiliate() 但它不起作用。

我正在考虑为此目的创建一个特定的服务,但我认为没有必要。我认为可以使用范围或其他类似的东西,但是我没有找到解决方案。

感谢任何简化此问题的建议、想法和解决方案。谢谢。

【问题讨论】:

  • 这更像是一个代码审查问题。

标签: php mysql laravel model affiliate


【解决方案1】:

试试这个:

Order::profitplatform()->profitaffiliate()->get();

【讨论】:

  • 错误:“在字符串上调用成员函数 Profit()”
猜你喜欢
  • 1970-01-01
  • 2017-10-25
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2020-04-05
  • 2016-10-11
  • 1970-01-01
相关资源
最近更新 更多