Php 如何将方法链接到模型当前状态?

Php 如何将方法链接到模型当前状态?,php,laravel,laravel-5,model,laravel-5.3,Php,Laravel,Laravel 5,Model,Laravel 5.3,我有一个模型,我得到的订单总数如下: public function total() { $total = $this->subtotal(); foreach ($this->lineItems as $l) { $total += $l->amount; } return $total; } 我想添加方法formatted(),它将格式化subtotal和total方法返回的函数number\u格式($numberher,2

我有一个模型,我得到的订单总数如下:

public function total()
{
    $total = $this->subtotal();
    foreach ($this->lineItems as $l) {
        $total += $l->amount;
    }
    return $total;
}
我想添加方法
formatted()
,它将格式化
subtotal
total
方法返回的函数
number\u格式($numberher,2)

我希望它是动态的,而不是像:
totalformatted
subtotalformatted
。我想像这样输出格式化的值:
$order->total()->formatted()


我有没有可能让它发挥作用?

将total创建为一个实例

protected $total;
然后把你的函数改成这个

public function total()
{
    $this->total = $this->subtotal();
    foreach ($this->lineItems as $l) {
      $this->total += $l->amount;
    }

    return $this;
}
然后创建格式化函数

public function formated()
{
  return number_format($this->total, 2)
}
public function formated()
{
  return [
      "total" => number_format($this->total, 2), 
      "subtotal" => number_format($this->subtotal, 2)
   ];
}
现在可以像下面这样链接函数

$order->total()->formated()
**已更新**

您可以在格式化函数中返回total和subtotal

public function formated()
{
  return number_format($this->total, 2)
}
public function formated()
{
  return [
      "total" => number_format($this->total, 2), 
      "subtotal" => number_format($this->subtotal, 2)
   ];
}
****

您可以对total和/或subtotal使用一个实例变量。让我们把这个变量命名为myTotals

protected $myTotals;

public function total()
{
    $this->myTotals = $this->subtotal();
    foreach ($this->lineItems as $l) {
      $this->myTotals += $l->amount;
    }

    return $this;
}


public function subTotal()
{
    $this->myTotals = $this->subtotal();
    foreach ($this->lineItems as $l) {
      $this->myTotals += $l->amount;
    }

    return $this;
}

public function formated()
{
    return number_format($this->myTotals, 2)
}
所以在这种情况下你可以打电话

$order->total()->formated() // and this will return the total
$order->subTotal()->formated() // and this will return the subtotal

将total创建为实例方差

protected $total;
然后把你的函数改成这个

public function total()
{
    $this->total = $this->subtotal();
    foreach ($this->lineItems as $l) {
      $this->total += $l->amount;
    }

    return $this;
}
然后创建格式化函数

public function formated()
{
  return number_format($this->total, 2)
}
public function formated()
{
  return [
      "total" => number_format($this->total, 2), 
      "subtotal" => number_format($this->subtotal, 2)
   ];
}
现在可以像下面这样链接函数

$order->total()->formated()
**已更新**

您可以在格式化函数中返回total和subtotal

public function formated()
{
  return number_format($this->total, 2)
}
public function formated()
{
  return [
      "total" => number_format($this->total, 2), 
      "subtotal" => number_format($this->subtotal, 2)
   ];
}
****

您可以对total和/或subtotal使用一个实例变量。让我们把这个变量命名为myTotals

protected $myTotals;

public function total()
{
    $this->myTotals = $this->subtotal();
    foreach ($this->lineItems as $l) {
      $this->myTotals += $l->amount;
    }

    return $this;
}


public function subTotal()
{
    $this->myTotals = $this->subtotal();
    foreach ($this->lineItems as $l) {
      $this->myTotals += $l->amount;
    }

    return $this;
}

public function formated()
{
    return number_format($this->myTotals, 2)
}
所以在这种情况下你可以打电话

$order->total()->formated() // and this will return the total
$order->subTotal()->formated() // and this will return the subtotal

只需将
$total
保存为属性(
$this->total+=$l->amount
),并在
total()方法的末尾返回类的当前实例=$this,基本上:

public function total()
{
    $this->total = $this->subtotal();
    foreach ($this->lineItems as $l) {
        $this->total += $l->amount;
    }

    return $this;
}
现在,您的方法返回类本身的实例,以便您可以调用另一个方法(或该类中的任何其他方法):

有了这个,你应该能够“链接”你的方法。关键是返回实例


$order->total()->>formatted()
您只需将
$total
保存为属性(
$this->total+=$l->amount
)并在
total()
方法结束时返回类的当前实例=$this,基本上:

public function total()
{
    $this->total = $this->subtotal();
    foreach ($this->lineItems as $l) {
        $this->total += $l->amount;
    }

    return $this;
}
现在,您的方法返回类本身的实例,以便您可以调用另一个方法(或该类中的任何其他方法):

有了这个,你应该能够“链接”你的方法。关键是返回实例


$order->total()->formatted()

没有什么是不可能的,但我建议不要这样做。对于这些与数据库无关的方法,您需要为您的模型添加大量额外的逻辑。也许可以看看这些类型的“计算属性”。然而,如果你真的想让它成为可链接的,我不想添加
number\u格式($numberher,2)
我想输出总额、小计、折扣或税额的每一行。这就是为什么我想在这里实现它。或者,如果你提出其他(更好的)解决方案?没有什么是不可能的,但我会建议反对。对于这些与数据库无关的方法,您需要为您的模型添加大量额外的逻辑。也许可以看看这些类型的“计算属性”。然而,如果你真的想让它成为可链接的,我不想添加
number\u格式($numberher,2)
我想输出总额、小计、折扣或税额的每一行。这就是为什么我想在这里实现它。或者如果你建议其他(更好的)解决方案?它只适用于total。我希望它也和小计一起工作。谢谢。@Buglinjo您是否尝试过应用相同的原则,但也在
subtotal
方法中?我不太明白您想如何使用subtotal。但我会更新我的答案,说明可能的解决方案。它只适用于total。我希望它也和小计一起工作。谢谢。@Buglinjo您是否尝试过应用相同的原则,但也在
subtotal
方法中?我不太明白您想如何使用subtotal。但我会更新我对可能解决方案的回答。