亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 基于關系值的排序模型

Laravel 基于關系值的排序模型

PHP
慕尼黑5688855 2023-11-03 10:49:14
我有一個名為“業務”的模型,一個企業可以提供多種服務。我有另一種模型,稱為“付款”。付款記錄了人們支付的服務費用。一項服務可以有多個付款。我打算根據收到的付款獲取排名前 10 名和排名最差的 10 家企業。下面的代碼工作正常,但效率很低。我必須循環遍歷整個數據才能檢索我需要的信息。有更有效的方法來實現這一目標嗎?$businesses = Business::with(['services'])->get();foreach($businesses as $business){    $id = $business->id;    $name = $business->display_name;    $services = $business->services;    $businessRevenue = 0;    if(count($services)>0){        foreach($services as $service){            $serviceId = $service->id;            $totalAmount = PaymentTransaction::whereHas('invoice', function($query) use ($serviceId){                $query->where('product_code_id', $serviceId);            })->where('amount_paid', ">", 0)->sum('amount_paid');            $businessRevenue= $businessRevenue + $totalAmount;        }    }    $businessArray = (object) array('id'=> $id, 'name'=> $name, 'revenue'=> $businessRevenue);        array_push($transformedBusiness, $businessArray);}$topBusiness = $bottomBusiness = $transformedBusiness;usort($bottomBusiness, function($a, $b) {return strcmp($a->revenue, $b->revenue);});usort($topBusiness, function($a, $b) {return strcmp($b->revenue, $a->revenue);});$topBusiness = array_slice($topBusiness, 0, 10);$bottomBusiness = array_slice($bottomBusiness, 0, 10);return view('report.department_performance', compact('topBusiness', 'bottomBusiness'));
查看完整描述

1 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

我想您可以使用聯接查詢直接從數據庫獲取排名前 10 的企業和排名最低的 10 家企業,而不是循環所有企業記錄并手動計算其收入


對于排名前 10 的業務,您可以對其余相關表使用內部聯接


$topBusinesses = DB::query()

      ->select('b.id', 'b.display_name',  DB::raw('sum(p.amount_paid) as revenue')

      ->from('business as b')

      ->join('service as s', 'b.id', '=', 's.business_id')

      ->join('invoice as i', 's.id', '=', 'i.product_code_id')

      ->join('payment_transaction as p', function ($join) {

                    $join->on('p.id', '=', 'i.payment_transaction')

                     ->where('p.amount_paid', '>', 0);

      })

      ->groupBy('b.id', 'b.display_name')

      ->orderByDesc('revenue')

      ->limit(10)

      ->get();

對于最低 10 個業務,請使用發票和 payment_transaction 的左聯接,這樣,如果這些表中沒有某個業務的記錄,您仍然可以獲得這些業務記錄


$lowestBusinesses = DB::query()

      ->select('b.id', 'b.display_name',  DB::raw('coalesce(sum(p.amount_paid),0) as revenue')

      ->from('business as b')

      ->join('service as s', 'b.id', '=', 's.business_id')

      ->leftJoin('invoice as i', 's.id', '=', 'i.product_code_id')

      ->leftJoin('payment_transaction as p', function ($join) {

                    $join->on('p.id', '=', 'i.payment_transaction')

                     ->where('p.amount_paid', '>', 0);

       })

      ->groupBy('b.id', 'b.display_name')

      ->orderBy('revenue')

      ->limit(10)

      ->get();

我使用 MySQL合并函數在返回 null 的情況下顯示 0 值sum(),如果您使用任何其他數據庫,您可以使用備用函數。


查看完整回答
反對 回復 2023-11-03
  • 1 回答
  • 0 關注
  • 154 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號