i have a column in my table it name is AccountBalance
i use laravel paginator with 30 rows per page when i use
$paginator->sum('AccountBalance')
it return Just sum of 30 rows in current page
i want to get sum of AccountBalance column in all pages
You can do sum before, paginate.
$balance = AccountBalance::all();
$total = $balance->sum('total');
$paginator = $balance->paginate(100);
return view("your.view",compact('total','paginator');
Related
I have a basic table with orders, which have a field thats called created_at which is in timestamp format and I want to get the avarage on how many orders have been created per day.
Found a other similar qestion about something like mine question whichI have posted below in the hope that everybody understand what im trying to do.
//not working
$q = new \yii\db\Query();
$q->from('shopping_cart');
$total = $q->average('DATEDIFF(`created_at`, CURRENT_DATE())');
I believe it is more SQL related problem than Yii2. What you need (if I have understood it correctly) is:
count number of days from the beginning to today
count all the rows
divide these number to get the average.
I have tried this and it works fine
SELECT
count(*) / (select round((unix_timestamp() - (select min(created_at) from table)) / 86400))
from table;
back to Yii2: I believe you have to build this query manually
\Yii::$app->db->createCommand()
Method average in $q->average('DATEDIFF('created_at', CURRENT_DATE())'); just adds AVG(DATEDIFF('created_at', CURRENT_DATE())) to SQL command.
As Jiri Semmler said, what you want to do is about SQL not Yii.
All you need to do is find the count of records for the period you are interested in and divide it by the number of days of that period.
It can be something like
// Define period
$timeFrom = '2018-11-30';
$timeTo = '2018-12-02';
// Number of days for the period
$days = date_diff(date_create($timeFrom), date_create($timeTo))->format("%a");
// Query count of records between dates
$q = new \yii\db\Query();
$total = $q->from('order')
->where(['between', 'created_at', $timeFrom, $timeTo])
->count();
// Find average records per day
$average = $total / $days;
If you have Order model class:
// Query count of records between dates
$total = Order::find()
->where(['between', 'created_at', $timeFrom, $timeTo])
->count();
i have table like this:
enter image description here
i want get sum from field 'total' from last 2 rows input,
this my code :
$data = DB::table('packagings')->where('plant',$pt->plant)->orderBy('id','desc')->limit(2)->sum('total');
but my code get all sum from field 'total'
Mysql will sum total from table packagings firstly that will become one record, and then take 2 records from one record. So it always return all sum from field total.
Collection way:
$data = DB::table('packagings')->where('plant',$pt->plant)->latest()->take(2)->get()->sum('total');
Query Builder way:
$sub = DB::table('packagings')->where('plant',$pt->plant)->latest()->take(2);
$data = DB::table(DB::raw("({$sub->toSql()}) AS sub"))->mergeBindings($sub)->sum('total');
I have a record in my database which looks like the below
Table
id client_id to_pay due session
1 12 100 50 ***
2 24 80 30 ***
3 12 0 10 ***
4 24 0 5 ***
Now in my query below, i want to fetch all data from the table above by grouping the data according to the client_id but in the query also, i need to sum up to_pay and due for each client in the response.
How do i get this done please ?
Controller
public function collectionPost(Request $request)
{
$fees = Collection::select('*')
->where('session',$request->get('session'))->groupby('client_id')->sum('due')
->get();
return $fees;
}
When you use the method sum it overwrites the select to get the right result.
You can build your select without using the sum method.
$fees = Collection::select(DB::raw('SUM(due) as total_due'), DB::raw('SUM(to_pay) as total_to_pay'))
->where('session',$request->get('session'))->groupby('client_id')
->get();
return $fees;
And add more to your select.
I have an orders table which holds orders.
Each orders can have supplements.
The relation is accessed via
$order->supplements;
Theres a column on the supplements table called 'unit_price'.
How do I get the total price of all supplemented combined?
Try using the laravel sum query.
$total = $order->supplements->sum('unit_price');
Should give you the total price
Try using sum aggregates()
$totalPrice = $order->sum(function ($query) {
return $query->supplements->sum('unit_price');
});
I go through this but its not working properly
$orders = Paginator::make($orders, $total, $per_page);
I have 2000 records in data base from that i want to take only 200 records total and per page I want 10 record I am using full text match query.
$data=Item::raw_where("match (`name`) against ('*{$search_text}*' IN BOOLEAN MODE)")->where_value('verified')->order_by('created_at', 'DESC')->paginate(10);
guys can any one help me regarding this..thanx in advance
Paginator::make($items, $total, $per_page) it has 3 parameters.
$items - record set of query result
$total - number of records into fetch result
$per_page - number of records per page you want
here, 2 step to perform manual pagination
step 1: count number of records first
$cc=Item::raw_where("match (`name`) against ('*{$search_text}*' IN BOOLEAN MODE)")->count();
if($cc>2000){$cc=2000;}
$per_page=200;
$page=Input::get('page',1);
step 2: paginate that records
$nn=Item::raw_where("match (`name`) against ('*{$search_text}*' IN BOOLEAN MODE)")->take($per_page)->skip(($page-1)*$per_page)->get();
$data=Paginator::make($nn, $cc, $per_page);
you will get paginated records in $data variable.