Quantcast
Channel: 一言多いプログラマーの独り言
Viewing all articles
Browse latest Browse all 846

Laravel5.5で、ショッピングカートの合計金額を取得

$
0
0
Laravel5.5で、ショッピングカートの合計金額を取得です。 いろいろな方法があると思いますが、エレガントな取得方法を見つけました。美しい(笑)。

/app/Cart.php

class Cart extends Model
{
// リレーション
public function item()
{
return $this->belongsToMany('App\Item')->withPivot('quantity');
}

// 合計金額集計
public function getTotalPrice() {
return $this->item()->sum(DB::raw('cart_item.quantity * items.price'));
}
}

参考サイト
Laravel Eloquent: Best Way to Calculate Total Price(Stack Overflow)

Viewing all articles
Browse latest Browse all 846

Trending Articles