Laravel 将访问器传递给 vue

Jam*_*mie 1 php laravel vue.js

在我的 Laravel 5.4 模型之一上,我有多个访问器。像这样:

public function getRevenueAttribute()
{
    return $this->calculate()->revenue($this->price, $this->amount);
}
Run Code Online (Sandbox Code Playgroud)

问题是当我将模型传递给我的 vue 组件时:

<product :prp-product="{{json_encode($product)}}"></product>
Run Code Online (Sandbox Code Playgroud)

product.revenue不存在。有没有办法实现这一目标?不想再在vue里计算这些东西了。

非常感谢。

mil*_*526 5

您需要向appends模型添加一个属性,请参阅了解更多信息。

/**
 * The accessors to append to the model's array form.
 *
 * @var array
 */
protected $appends = ['revenue'];
Run Code Online (Sandbox Code Playgroud)