【问题标题】:Laravel datatabel taking too longLaravel 数据表耗时过长
【发布时间】:2021-08-05 04:27:22
【问题描述】:

所以,这是我的代码,用于评估总和为 0 的结果,

 $clients = Client::with('transactions')->where('trash', 0)->get();
 $data = [];
 foreach($clients as $client_data){
 $type_one=collect($client_data->transactions)->where('type',1)->sum('point');
 $type_two =collect($client_data->transactions)->where('type',2)->sum('point');
 $cal = $type_one - $type_two;
 if($cal != 0){
     $data [] = $customer_data;
     } 
  }

如何让加载更清晰??

【问题讨论】:

    标签: php laravel datatable


    【解决方案1】:

    试试服务端数据表,有时取数据需要时间

    【讨论】:

      【解决方案2】:

      我解决了这个问题。通过ajax加载数据表数据 Datatable Doc

      【讨论】:

        【解决方案3】:
        $clients = Client::withSum(['transactions as type_one' => function (Builder $query) {
                        $query->where('type', 1);
                    }], 'point')->withSum(['transactions as type_two' => function (Builder $query) {
                        $query->where('type', 2);
                    }], 'point')
                ->where('trash', 0)
                ->get();
        

        您可以使用 withSum Aggregate 方法来使用关系方法,而不是在 Foreach 中这样做。

        我个人建议你创建一个mysql视图,只有在$cal = $type_one - $type_two;之后才能获得记录

        【讨论】:

          猜你喜欢
          • 2020-07-17
          • 2020-02-27
          • 1970-01-01
          • 2017-12-06
          • 2014-11-07
          • 1970-01-01
          • 2012-10-24
          • 2013-06-16
          • 2013-02-26
          相关资源
          最近更新 更多