Laravel Sum column database Eloquent - mysql

Trying to get the sum of a int field in one of my table should be pretty easy, unfortunately it is not as I'm getting different result whether I use Laravel, MySQL or Excel.
Laravel 5.4 gives me 20506:
Table::sum('field_name');
MySQL gives me 1830:
Select sum(field_name) from table;
And the data from the Excel sheet before importing it into the database:
Sum gives me 145689
Any idea? I tried to cast to integer before doing the sum but it doesn't work.
All the numbers are pretty big but don't contain comma or dot.
Examples of values I have to sum: (contain sometimes empty cells)
17906774
99630157
28581131
159551532
20312892
668928885

$query = YourModel::query();
$query->withCount([
'activity AS yoursum' => function ($query) {
$query->select(DB::raw("SUM(amount_total) as paidsum"))->where('status', 'paid');
}
]);

You can use laravel aggregate function SUM as :
$result = DB::table('table_name')
->select(DB::raw('SUM(field_name) as total_field_name'))
->get();
For more details you can follow:
https://laravel.com/docs/5.4/queries
Thanks

Try with
$result = DB::table(tablename)
->selectRaw('sum(column)')
->get();
If it still gives you wrong result, maybe wait for someone to give you a better answer. This is all I could think of.

Related

Concatenate 2 columns laravel

I am trying to concatenate 2 columns and researched how to do it but I don't know where the error is, it says column not found, this is my query
public function obtenerCargo() {
if ($this->rcargo_id == null) {
$this->listCargo = recepcionCargo::select(
'recepcion_cargo.id',
'recepcion_cargo.rcargo_id',
'recepcion_cargo.no_factura',
'cargo_id.cargo_id',
'recepcion_cargo.porcentaje',
'recepcion_cargo.cargo_minimo',
'recepcion_cargo.cargo_devolucion',
'recepcion_cargo.cargo_reparacion',
'recepcion_cargo.cargo_almacenaje',
'recepcion_cargo.cargo_visita',
'recepcion_cargo.cargo_traslados',
'recepcion_cargo.cargo_fletes',
'recepcion_cargo.total',
'recepcion_cargo.cargo_porcentaje',
'recepcion_cargo.total_sp',
DB::raw("CONCAT(recepcion_cargo.total,' ',recepcion_cargo.total_sp) AS TOTAL",'recepcion_cargo.id'))
->pluck('TOTAL', 'recepcion_cargo.id')
->join('cargo_id', 'cargo_id.id', '=', 'recepcion_cargo.car_id')->get();
} else {
$this->listCargo = recepcionCargo::select(
'recepcion_cargo.id',
'recepcion_cargo.rcargo_id',
'recepcion_cargo.no_factura',
'cargo_id.cargo_id',
'recepcion_cargo.porcentaje',
'recepcion_cargo.cargo_minimo',
'recepcion_cargo.cargo_devolucion',
'recepcion_cargo.cargo_reparacion',
'recepcion_cargo.cargo_almacenaje',
'recepcion_cargo.cargo_visita',
'recepcion_cargo.cargo_traslados',
'recepcion_cargo.cargo_fletes',
'recepcion_cargo.total',
'recepcion_cargo.cargo_porcentaje',
'recepcion_cargo.total_sp',
DB::raw("CONCAT(recepcion_cargo.total,' ',recepcion_cargo.total_sp) AS TOTAL",'recepcion_cargo.id')
)
->pluck('TOTAL', 'recepcion_cargo.rcargo_id')
->join('cargo_id', 'cargo_id.id', '=', 'recepcion_cargo.car_id')
->where('recepcion_cargo.rcargo_id', '=', $this->rcargo_id)->get();
}
}
I am using my model and db:raw with pluck according to this it should work but it does not, in my view a table is displayed and it is not convenient to have 2 total fields so it is better for me to use concat
What is happening in your queries is:
You are selecting a lot of columns from a table, including one that belongs to another table and a DB::raw statement
You are performing the query and extracting the fields TOTAL and recepcion_cargo.id (that is what pluck does, it first performs the query, then extracts the results, so at this point your query will fall)
You are joining a Illuminate\Support\Collection to another table
I would recommend a couple actions:
First, you should join before executing the query (that is, before calling pluck
Second, to improve the performance, only select the columns that you are going to use
Hope it helps. Good luck!

mysql queries whose results are not quite right

I have a database :
I want to retrieve data with conditions where the card number is 7689, with product ID 73 or 71.
$this->cekModel->where('card_number', 7689)->where('id_product ', '73')orWhere('id_product', '71')->FindAll();
The result must display 2 data, i.e. which has id = 1 and id = 4 but I only get one data using the query above
Doesn't whereIn() do what you want?
$this->cekModel
->where('card_number', 7689)
->whereIn('id_product', array(71, 73))
->FindAll();
$this->cekModel
->where('card_number', 7689)
->whereIn('id_product', array(71, 73))
->FindAll();
The code above does not work correctly on my console (returns wrong results).
I tried modification and it worked.
$data=['71','73'];
$this->cekModel
->where('card_number', 7689)
->whereIn('id_product', $data)
->FindAll();
Thank you..

Not retrieving set of values from table. Laravel

I'm trying to get values from my linking table sfees with columns student_id and mfee_id. Here, there might be multiple student_id with different mfee_id. The thing is that, i want to retrieve all mfee_id with same student_id.
I have used following syntax, but it is only returning single value:
public function verify($id,$sid)
{
$sfees = sfee::where('student_id', $sid)->value('mfee_id');//trying to get only mfee_id
return $sfees;
}
How can i solve this problem?
//edited
My table looks like:
You need to do a groupBy -
$sfees = sfee::where('student_id', '=',$sid)->groupBy('student_id')->get();
UPDATE
Try something like this -
$sfees = sfee::where('student_id', '=',$sid)->lists('mfee_id');
Or you can use the Schema Builder like this -
DB::table('sfees')->where('student_id', '=', $id)->lists('mfee_id');

Laravel eloquent how to parse data inside the query where attribute

I have following issue.
I need to get rows from database where price is lower than given number.
I can do it this way:
DB::where('price', '<=', $given_number)->get();
but the problem is that inside price cell I have number with added signs like $ for dollars or Euro sign. I know how to parse the string in PHP to extract only numbers - filter_var($string, FILTER_SANITIZE_NUMBER_INT) , but how can I do this inside the query from Laravel? How can i pre-parse this 'price' attribute?
Is this what you mean?
$price = filter_var($given_number, FILTER_SANITIZE_NUMBER_INT);
$products = Product::where('price', '<=', $price)->get();
Is there more information that you left out?

Get ABS(absolute value) in laravel Query

I'm new to Laravel framework and I'm looking for how to write Query in order to get the absolute value(in short ignoring the + and - sign and only display number from database.
I know we can use Abs in order to get it. But I don't know how do it.
Take a look at my code:
$users = DB::table('transaction_details')->
Join('ledger','transaction_details.ledger','=','ledger.Name')->
groupBy('ledger.Name')->select(
'ledger.CrDr as CrDr',
'transaction_details.ledger as Name',
'transaction_details.amount as Debit',
'ledger.OpeningBalance as openingBalance'
)->get();
In above query i want ledger.openingBalance value to be absolute.
How do I do that?
You can use DB::raw for the ledger.openingBalance select column to avoid it from being quoted by the Query Builder:
DB::table('transaction_details')
->join('ledger','transaction_details.ledger','=','ledger.Name')
->groupBy('ledger.Name')
->select(
'ledger.CrDr as CrDr',
'transaction_details.ledger as Name',
'transaction_details.amount as Debit',
DB::raw('ABS(ledger.OpeningBalance) as openingBalance')
)->get();