Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel sum eloquent collection

How can i sum a data set that has been eager loaded?

This is my table structure:

regions table
+------------+------------------+------+-----+---------------------+----------------+
| Field      | Type             | Null | Key | Default             | Extra          |
+------------+------------------+------+-----+---------------------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| region     | varchar(255)     | NO   |     | NULL                |                |
| created_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+------------+------------------+------+-----+---------------------+----------------+

submits table
+------------+------------------+------+-----+---------------------+----------------+
| Field      | Type             | Null | Key | Default             | Extra          |
+------------+------------------+------+-----+---------------------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| region_id  | int(11)          | NO   |     | NULL                |                |
| deals      | int(11)          | NO   |     | NULL                |                |
| created_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+------------+------------------+------+-----+---------------------+----------------+

These are my models / relationships :-

class Region extends Eloquent {

  public function submits()
  {
    return $this->hasMany('Submit');
  }

}

<?php

class Submit extends Eloquent {

  public function regions()
  {
    return $this->belongsTo('Region');
  }
  
}

This is my controller

  public function index()
  {
    $regions = Region::with('submits')->get();

    return $regions;
    //return View::make('pages.index', compact('regions'));
  }

This is what the returned data looks like (in json format, i understand $regions in an eloquent collection):-

raw json

I can't figure out how i can send the sum total of 'deals' (4) back to the view?

like image 798
Lovatt Avatar asked Jan 06 '15 17:01

Lovatt


People also ask

How do you find the sum in Laravel eloquent?

First is using the Model and sum method of eloquent. Second is Model with select and Db::raw class. In the third example we have used Query Builder DB class and table function to use sum method of it. And final example is query builder with select and db raw to use sum method of MySQL or SQL.

How do I combine two collections in Laravel?

Laravel collection merge() method merge any given array to first collection array. If the first collection is indexed array, the second collection will be added to the end of the new collection. The merge() method can accept either an array or a Collection instance.

How do you find the total of a column in Laravel?

$balance = DB::table('data')->sum('balance'); which gets the sum of the entire column.

What is pluck in Laravel?

Laravel Pluck() is a Laravel Collections method used to extract certain values from the collection. You might often would want to extract certain data from the collection i.e Eloquent collection.


1 Answers

$deals = $regions->sum(function ($region) {
    return $region->submits->sum('deals');
});
like image 108
Joseph Silber Avatar answered Sep 23 '22 11:09

Joseph Silber