Laravel-запрос-grouByMonth-groupByYear-sumdata - PullRequest
2 голосов
/ 15 апреля 2019

помогите пожалуйста решить эту проблему.

я хочу вывести группу данных по месяцу и году, а затем также сумму каждого итога за месяц

это мой код

$id = Auth::user()->adoptor_id;
        $posts_dates = Sale::where('adoptor_id',$id)->whereYear('created_at',date('Y'))
                        ->orderBy( 'created_at', 'ASC' )
                        ->sum('total')
                        ->pluck( 'created_at','total')
                        ;
        if ( ! empty( $posts_dates ) ) {
            foreach ( $posts_dates as $unformatted_date ) {
                $date = new \DateTime( $unformatted_date);
                $month_no = $date->format( 'm' );
                $month_name = $date->format( 'M-Y' ); 
                $total = $posts_dates->total;
                $month_array[ $month_no ] = $month_name;

                echo '<tr>
                <td>'.$month_name.'</td>
                <td>'.$total.'</td>
                </tr>';
            }


        } ```

the out put should be like this

|monthandYear| |totalSale|
  jan 2019        5455
  Feb 2019         545
  Apr 2019         454



please help me to this problem . Thank you in advance.!

1 Ответ

0 голосов
/ 06 мая 2019

`защищенная функция getAllMonths () { $ id = Auth :: user () -> accepttor_id; $ month_array = array ();

    $posts_dates = Sale::where('adoptor_id',$id)->whereYear('created_at',date('Y'))
                    ->orderBy( 'created_at', 'ASC' )
                    ->pluck( 'created_at');
    $posts_dates = json_decode( $posts_dates );
    if ( ! empty( $posts_dates ) ) {
        foreach ( $posts_dates as $unformatted_date ) {
            $date = new \DateTime( $unformatted_date);
            $month_no = $date->format( 'm' );
            $month_name = $date->format( 'F-Y' );
            $month_array[ $month_no ] = $month_name;
        }
    }
    return $month_array;
}
protected function getMonthlyPostCount( $month ) {
    $year = date('Y');
    $id = Auth::user()->adoptor_id;
    $monthly_post_count = Sale::where('adoptor_id',$id)
                        ->whereYear('created_at',$year)
                        ->whereMonth('created_at', $month )
                        ->sum('total');
    return $monthly_post_count;
}
protected function salesData() {
    $monthly_post_count_array = array();
    $month_array = $this->getAllMonths();
    $month_name_array = array();
    if ( ! empty( $month_array ) ) {
        foreach ( $month_array as $month_no => $month_name ){
            $monthly_post_count = $this->getMonthlyPostCount( $month_no );
            array_push( $monthly_post_count_array, $monthly_post_count );
            array_push( $month_name_array, $month_name );

            echo '<tr>
                    <td>'.$month_no.'</td>
                    <td>'.$month_name.'</td>
                    <td class="badge badge-success">'.number_format($monthly_post_count).'</td>
                </tr>';

        }
    }
    $max_no = max( $monthly_post_count_array );
    $max = round($max_no * 1.5);
}`

затем отобразите его на графике, используя диаграмму js в качестве массива для страницы блейда

...