【问题标题】:Get sum of multiplication of two column values from unrelated tables in cart从购物车中不相关的表中获取两列值相乘的总和
【发布时间】:2018-04-28 23:22:03
【问题描述】:

在我的 laravel 电子商务项目中 我在数据库中有 2 个表:

 cart [c_id(pk), laundry, l1, dryclean, dc1, dc2, dc3, shop_id]
 price [p_id, p_l1, p_dc1, p_dc2, p_dc3, shop_id]

在控制器中,我得到登录商店的价格,即单行

$price = DB::table('price')->where('price.shop_id', '=', auth()->id())->get();

还要从购物车表中获取一行,我正在获取最新条目

$cart = DB::table('cart')->latest()->first();

购物车表中的洗衣和干洗列可以有两个值 YES 和 NO。如果洗衣是肯定的,那么用户也输入 l1(这是数量),否则为空。

类似地,对于干洗柱可以有两个值 YES 和 NO。如果是,则用户还输入 dc1、dc2、dc3(项目数量)。

现在我想从控制器获取结帐页面上的总金额,包括检查洗衣和干洗值的条件。

到目前为止,我正在计算视图文件中的总数。

@if ( $cart->dryclean == "no")
    @php $c1=0;$c2=0;$c3=0; @endphp
@else
      @if (is_null($cart->dc1))
      @php  $c1=0; @endphp
      @else
            @php
            $a1= $cart->dc1;
            $b1 =$price->p_dc1;
            $c1= $a1*$b1;
            @endphp
      @endif
      @if (is_null($cart->dc2))
      @php  $c2=0; @endphp
      @else
            @php
            $a2= $cart->dc2;
            $b2 =$price->p_dc2;
            $c2= $a2*$b2;
            @endphp
      @endif
      @if (is_null($cart->dc3))
      @php  $c3=0; @endphp
      @else
            @php
            $a3= $cart->dc3;
            $b3 =$price->p_dc3;
            $c3= $a3*$b3;
            @endphp
      @endif

      {{ $c1 + $c2 + $c3}}   <!-- This is total amount -->
      @endif

请帮助我编写控制器查询以计算控制器本身的总量并显示在视图中。

提前致谢。

【问题讨论】:

  • 您能否分享一些您的视图文件中的代码,说明您是如何计算的?
  • 我已经从视图文件中发布了一些代码。
  • 我的回答有没有给你你需要的东西?如果是,你能把它标记为正确吗?

标签: laravel laravel-5 eloquent


【解决方案1】:

只需删除您所做的一切的刀片语法。

if ( $cart->dryclean == "no" ) {

    $c1 = 0;
    $c2 = 0;
    $c3 = 0;

} else {

    if ( is_null($cart->dc1) ) {

        $c1 = 0; 

    } else {

        $a1 = $cart->dc1;
        $b1 = $price->p_dc1;
        $c1 = $a1 * $b1;

    } 

    if ( is_null($cart->dc2) ) {

        $c2 = 0;

    } else {

        $a2 = $cart->dc2;
        $b2 = $price->p_dc2; 
        $c2 = $a2 * $b2;

    } 

    if ( is_null($cart->dc3) ) {

        $c3 = 0;

    } else {

        $a3 = $cart->dc3;
        $b3 = $price->p_dc3;
        $c3 = $a3 * $b3;

    }

    // total used to be calculated here

}

$total = $c1 + $c2 + $c3;


return view('your.view.here')->with('total', $total);

根据我看到的一些提示:

  • 在 $cart->dryclean 字段中使用布尔值。这比“是”和“否”更容易存储和使用。
  • 您在第一个“if”语句结束之前计算刀片中的总数。如果干洗值为否(或错误),则不会给出总数。
  • 为了您自己的可读性,我建议使用全名,例如$cost1。实际上我什至无法判断这是否是您的意图,因为它只是说'c1'..
  • 如果第一个为空,您可以通过使用空合并运算符给出默认值来绕过许多计算。

如果你接受我的建议,你可以让你的控制器像下面的例子。

if ( $cart->dryclean ) {

    $cost1 = ($cart->dc1 ?? 0) * $price->p_dc1;

    $cost2 = ($cart->dc2 ?? 0) * $price->p_dc2;

    $cost3 = ($cart->dc3 ?? 0) * $price->p_dc3;

} else {

    $cost1 = 0;
    $cost2 = 0;
    $cost3 = 0;

}

$total = $cost1 + $cost2 + $cost3;

return view('your.view.here')->with('total', $total);

如果你想更进一步,你可以这样做(使用三元运算符):

// set class properties
$this->cart = $cart;
$this->price = $price;

$total = ( $cart->dryclean )
         ? $this->costOf('1') + $this->costOf('2') + $this->costOf('3')
         : 0;

return view('your.view.here')->with('total', $total);

当然,这是使用原始控制器功能之外的另一个私有功能:

private funtion costOf($number)
{
return ( $this->cart->{'dc'.$number} ?? 0 ) * $this->price->{'p_dc'.$number};
}

这个costOf 函数正在使用数字的字符串版本来获取每个模型类型上的关联字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2015-01-26
    相关资源
    最近更新 更多