Get total value of one column from a collection - php

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');
});

Related

Laravel get SUM Of columns in all pages in paginator

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');

Get SUM form last 2 rows input with laravel

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');

how to select table data with different values of single column in codeigniter

following code is about getting the products from my db table using codeigniter sql queries.i am getting products of a row by limiting up to 4 products and by applying where condition of products less than cost of 900 but i am not getting how to get the products less than 900 but with different prices in each item. this means if once product 0f 500 is fetched it should not fetch 500 again it should go for another product by the help of product_id DESC. Explain me the logic of query how i should write
public function byPrice()
{
$query = $this->db->limit(4)
->where('pro_cost<', 900)
->get('products');
return $query;
}
$Q="SELECT DISTINCT `pro_cost` FROM `products` WHERE `pro_cost` < 900";
$result=$this->db->query($Q)->result();
$this->db->where('cost < 900', NULL)->group_by('id')->group_by('cost')->get('product')

Get qty of products in an order

i would like to know how to get the unique quantity of products in a order
i have tried these (in /renderer/default.phtml):
$_order = $this->getOrder();
$qnt = round($_order->getData('total_qty_ordered'), 0);
but this returns the total number of products, and i need only total of different products.
thanks!
The problem is that an order may contain many products. Hence qty of a product is not assigned at order level but is is assigned at item level. You can get that as below:
$_order = $this->getOrder();
foreach ($_order->getAllItems() as $items){
$qty = $items->getQty();
}
You can get the number of different products by loading the visible items of an order (since Magento stores two order items for configurable products, one for the parent and one for the child, this method provides only one item in such cases) and count the number of array entries.
For example in this way:
$_order = $this->getOrder();
$qnt = count($_order->getAllVisibleItems());

How to retrieve column total when rows are paginated?

I have a column "price" in a table and I used a pagination script to generate the data displayed. Now the pagination is working perfectly however I am trying to have a final row in my HTML table to show the total of all the price. So I wrote a script to do just that with a foreach loop and it sort of works where it does give me the total of all the price summed up together however it is the sum of all the rows, even the ones that are on following pages. How can I retrieve just the sum of the rows displayed within the pagination?
Here is the query..
SELECT
purchase_log.id,
purchase_log.date_purchased,
purchase_log.total_cost,
purchase_log.payment_status,
cart_contents.product_name,
members.first_name, members.last_name,
members.email FROM purchase_log LEFT
JOIN cart_contents ON purchase_log.id
= cart_contents.purchase_id LEFT JOIN members ON purchase_log.member_id =
members.id GROUP BY `id` ORDER BY `id` DESC LIMIT 0,30";
Just use MySQL function FOUND_ROWS(): http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
You need to append LIMIT 0,20 to a mysql query this will only sum total the current pageinated displayed results.
I assume you use a LIMIT to pageinate the results, unless you array the data into a grid in which case you'll need to post code.
What about using the mysql SUM function in your mySQL query?
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_sum
First you need to initialize total to zero so that it can display the sum of the records that you are fetching with limit.
<?php
$total = 0;
foreach ($purchase as $items) :
$total += $items['purchase_log.total_cost'];
endforeach;
?>
Hope this helps.

Categories