【问题标题】:making report in laravel By Day Month Year按日月年在laravel中制作报告
【发布时间】:2021-06-02 21:35:11
【问题描述】:

我的代码总和产品以及数量和价格,但我首先需要 3 个函数用于年月的一天,我将在每个月的每一天和每年的每一天获得产品总数

守则

$product_orders = \App\OrderProduct::select(DB::raw('sum(line_total) as total_line'),
DB::raw('sum(qty) as qty') , 'product_id','line_total','created_at')->
orderBy('created_at')->groupBy('product_id')->get();

数据库

id , prdouct_id ,order_id, qty ,unit_total , line_total , created_at , updated_at


【问题讨论】:

    标签: php laravel


    【解决方案1】:
    ##for grouping you can use it like these
    
    public function day(){
    $products= OrderProduct::select(
                    DB::raw('DAY(created_at) as day'),DB::raw('sum(line_total) as total_line') , 'product_id','qty','line_total','created_at')->groupBy('day','product_id')->get();
    }
    public function month(){
    $products= OrderProduct::select(
                    DB::raw('MONTHNAME(created_at) as month'),
                    DB::raw('sum(line_total) as total_line') , 'product_id','qty','line_total','created_at')->groupBy('month','product_id')->get();
    }
    public function year(){
    $products= OrderProduct::select(
                    DB::raw('YEAR(created_at) as year'),
                   DB::raw('sum(line_total) as total_line') , 'product_id','qty','line_total','created_at')->groupBy('year','product_id')->get();
    }
    

    试试这个,让我知道发生了什么

    【讨论】:

    • 有产品没有退回的例子,只有2个退回的产品,请您再检查一下
    • 你能把数据库表发给你,这样我就可以确保......我认为答案还可以
    • 我的意思是数据......我想显示所有行......为什么不显示数据
    • 通过在每个组中添加 rpdocut_id 来解决它,请更新您的答案
    • 很高兴知道您对我的回答有所帮助
    【解决方案2】:

    试试这个:

    $Rspatients = DB::table('reports')
    ->select(
        DB::raw("day(created_at) as day"),
        DB::raw("Count(*) as total_patients"))
    
    ->orderBy("created_at")
    ->groupBy(DB::raw("day(created_at)"))
    ->get();
    
    
    $result_patients[] = ['day','Patients'];
    foreach ($Rspatients as $key => $value) {
    $result_patients[++$key] = [$value->day,$value->total_patients];
    }
    

    然后你可以像这样将它传递给你的视图:

    return 
    view('Dashboard.index')
            ->with('result_patients',json_encode($result_patients));
    

    我使用谷歌图表,这就是为什么我将我的数据作为 json 发送到视图,然后将数据馈送到谷歌图表的折线图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多