私はこのの助けを借りて、注文の合計を計算しようとしました: Laravel Eloquent: Best Way to Calculate Total PriceLaravel料金の計算
私は計算がモデルで行うことにしたいので、私は複数のコントローラでそれを使用することができます。
私のコードは次のようになります。
class Order extends Model
{
public function customer()
{
return $this->belongsTo('App\Customer');
}
public function orderItems()
{
return $this->hasMany('App\OrderItem');
}
public function total()
{
return $this->orderItems->sum(function($orderItem)
{
return $orderItem->net_price;
});
}
}
だから私は、私は正確に例のようにすべてを行っていると思うが、私は次のエラーを取得しています:
はErrorException in Model.php line 2696: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
すべてのヘルプは非常になります感謝。
ありがとうございます。
今はまったくそれを呼び出していないとにかくエラーが発生します。 – Joha
私は{{$ order-> total}}と{{$ order-> total()}}のビューから呼び出す前に – Joha