Kareimovich
Kareimovich

Reputation: 707

making report in laravel By Day Month Year

My Code Sum Products and quantity and price but i need 3 functions for day year month in first i will get total of products in every day next in every month and last in every year

the Code

$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();

DB

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


Upvotes: 0

Views: 1486

Answers (2)

Sabaoon Bedar
Sabaoon Bedar

Reputation: 3689

try this:

$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];
}

then you can pass it to your view like this:

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

I used google charts that is why I sent my data as a json to the view, the data is then feed to the line chart of google chart.

Upvotes: 0

Tanvir Ahmed
Tanvir Ahmed

Reputation: 1019

##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();
}

try this and let me know what happened

Upvotes: 2

Related Questions