Laravel Sum Eloquent来自两个不同表

时间:2018-04-30 01:21:19

标签: php laravel laravel-5 eloquent sum

目前,在我的Laravel项目控制器中,我正在使用一个查询

QUERY 1

public function cartreview(Request $request,$sp_id, $service_id,$cart_id)
    {
    $total = DB::table('pricings')
    ->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
    ->select(DB::raw('sum(pricings.shirt*carts.quantity_shirt ) AS total'))                      
    ->where('pricings.sp_id', '=', $sp_id)
    ->where('carts.id', '=' , $cart_id)
    ->first();
    }

在上面的查询中,我使用两个数据库表作为 pricings 推车 我通过获取定价表格和购物车数量表来计算衬衫物品的总票据价格。

现在我还要添加另一件带衬衫的项目,如裤子,领带等。 如何将更多乘法传递给总和?

请帮我解释一下语法。我可以做这样的事吗

QUERY 2

    $total = DB::table('pricings')
        ->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
        ->select(DB::raw('sum(pricings.shirt*carts.quantity_shirt ,
                             pricings.pant*carts.quantity_pant , 
                             pricings.tie*carts.quantity_tie) AS total'))                      
        ->where('pricings.sp_id', '=', $sp_id)
        ->where('carts.id', '=' , $cart_id)
        ->first();

或者即使我为每个项目分别计算总数我该如何添加?

$total_shirt = DB::table('pricings')
    ->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
    ->select(DB::raw('sum(pricings.shirt*carts.quantity_shirt ) AS total_shirt'))                      
    ->where('pricings.sp_id', '=', $sp_id)
    ->where('carts.id', '=' , $cart_id)
    ->first();

   $total_pant = DB::table('pricings')
    ->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
    ->select(DB::raw('sum(pricings.pant*carts.quantity_pant ) AS total_pant'))                      
    ->where('pricings.sp_id', '=', $sp_id)
    ->where('carts.id', '=' , $cart_id)
    ->first();

   $total_tie = DB::table('pricings')
    ->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
    ->select(DB::raw('sum(pricings.tie*carts.quantity_tie ) AS total_tie'))                      
    ->where('pricings.sp_id', '=', $sp_id)
    ->where('carts.id', '=' , $cart_id)
    ->first();

$ total = $ total_shirt + $ total_pant + $ total_tie; ?

要在view.blade.php中显示值,请使用{{$ total-> total}}

之类的内容

提前致谢。

TRIED:

$waftotal = DB::table('pricings')->join('carts', 'carts.sp_id', '=', 'pricings.sp_id')
                  ->select(DB::raw('sum(
                                        pricings.Regular_Laundry*carts.q_Regular_Laundry,
                                        pricings.Bedding_Mattress_Duvet_Cover*carts.q_Bedding_Mattress_Duvet_Cover,
                                        pricings.Bedding_Comforter_laundry*carts.q_Bedding_Comforter_laundry,
                                        pricings.Bedding_Blanket_Throw*carts.q_Bedding_Blanket_Throw,
                                        pricings.Bedding_Pillow_laundry*carts.q_Bedding_Pillow_laundry,
                                        pricings.Bath_Mat_laundry*carts.q_Bath_Mat_laundry,
                                        pricings.Every_Hang_Dry_Item*carts.q_Every_Hang_Dry_Item
                                        ) AS waftotal'))
                  ->where('pricings.sp_id', '=', $sp_id)->where('carts.id', '=' , $cart_id)->first();

但它给我的错误

SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与您的MariaDB服务器版本对应的手册,以便在第1行的'pricings.Bedding_Mattress_Duvet_Cover c'附近使用正确的语法(SQL:select sum(pricings.Regular_Laundry carts.q_Regular_Laundry,pricings.Bedding_Mattress_Duvet_Cover < EM> carts.q_Bedding_Mattress_Duvet_Cover,pricings.Bedding_Comforter_laundry carts.q_Bedding_Comforter_laundry,pricings.Bedding_Blanket_Throw carts.q_Bedding_Blanket_Throw,pricings.Bedding_Pillow_laundry carts.q_Bedding_Pillow_laundry,pricings.Bath_Mat_laundry carts.q_Bath_Mat_laundry,pricings。 Every_Hang_Dry_Item carts.q_Every_Hang_Dry_Item)来自pricings的{​​{1}}内部加入carts的来自carts = sp_idpricings其中{ {1}}。sp_id = 1和pricingssp_id = 23限制1)

即使我写了单独的查询

carts

在view.blade.php {{$ waftotal}}或{{$ waftotal-&gt; waftotal}}中,我试图获取非对象的属性。

建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

first()将返回一个对象,因此您需要添加每个对象的属性: https://laravel.com/docs/5.6/eloquent#retrieving-single-models

而不是$total = $total_shirt + $total_pant + $total_tie;

它将是$total = $total_shirt->total_shirt + $total_pant->total_pant + $total_tie->total_tie;,因为您已将每个对象的总和分配给以该对象命名的属性。

但是你的第一个组合查询应该可以正常工作。你得到什么错误?

答案 1 :(得分:0)

$maintotal = DB::table('carts')
           ->join('products', 'carts.productid', '=', 'products.id')
           ->select(DB::raw('sum(products.price*carts.quantity) AS maintotal3'))
           ->where('carts.userid', '=', $useri)->get();

  @foreach($maintotal as $t)
     {{$t->maintotal3}}
  @endforeach