Stank0V01
1 year ago

Getting monthly price sum from specific year

Posted 1 year ago by Stank0V01

Hello guys.. I use that for my chart https://github.com/fxcosta/laravel-chartjs..

I have orders table with that columns

/product_id
/user_id

I have hasMany relationship between Order and Product models

My Order mode:

<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $fillable = ['product_id', 'm_c_user_id', 'site_id'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(MCUser::class, 'm_c_user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    /**
     * @return int
     */
    public function getTotalPrice($siteID)
    {

        $orders = self::with('product')->where('site_id', $siteID)->get();
        $total = $orders->sum('product.price');
        return $total;
    }

    /**
     * @return mixed
     */
    public function getTodayTurnover($siteID)
    {
        $orders = self::with('product')->where('site_id', $siteID)->get();
        $total = $orders->where('created_at', '>=', Carbon::today())->sum('product.price');

        return $total;
    }

}

I want to make something like that http://prntscr.com/kk3ude

To get for each mounth total revenue! Please help!

Please sign in or create an account to participate in this conversation.