Adding an object to a collection

Posted 4 years ago by jediping

Relative newbie to Laravel. (And programming in general.) :)

I have a collection called FundingSources. Each funding source has a fiscal year and a type of funding. There may be more than one object in the collection with the same fiscal year (but a different type_of_funding), but there may be only one. The start of the var_dump for one of the funding sources looks like:

 55 => 
    object(App\FundingSource)[203]
      protected 'guarded' => 
        array (size=0)
          empty
      protected 'connection' => null
      protected 'table' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=10)
          'id' => int 2
          'fiscal_year' => string '10' (length=2)
          'type_of_funding' => string 'impact' (length=6)
          'slug' => string 'fy10-impact' (length=11)
          'funded' => string '4140348.00' (length=10)
          'spent' => string '3387850.00' (length=10)
          'obligated' => string '478337.00' (length=9)
          'impact_fee' => string '213554.00' (length=9)
          'created_at' => string '2015-06-09 19:14:35' (length=19)
          'updated_at' => string '2015-06-09 19:14:35' (length=19)
      protected 'original' => 
        array (size=10)
          'id' => int 2
          'fiscal_year' => string '10' (length=2)
          'type_of_funding' => string 'impact' (length=6)
          'slug' => string 'fy10-impact' (length=11)
          'funded' => string '4140348.00' (length=10)
          'spent' => string '3387850.00' (length=10)
          'obligated' => string '478337.00' (length=9)
          'impact_fee' => string '213554.00' (length=9)
          'created_at' => string '2015-06-09 19:14:35' (length=19)
          'updated_at' => string '2015-06-09 19:14:35' (length=19)
   etc....

When there are multiple objects with the same fiscal year, I need to total up the various funded, spent, obligated, and impact_fee amounts. I'm struggling to find a way to do this easily, so what I've come up with so far is this:

$funding_sources = FundingSource::all();
        //Get the possible fiscal years
        $this->fiscal_years = FundingSource::distinct()->lists('fiscal_year');

        //Add a "totals" attribute for each fiscal year to the collection
        foreach($this->fiscal_years as $fiscal_year){
            $this->new_item=[SOMETHING HERE???];
            $this->new_item->fiscal_year = $fiscal_year;
            $this->new_item->type_of_funding = 'total';
            $this->new_item->slug = $fiscal_year.'_total';
            $this->new_item->funded = 0;
            $this->new_item->spent = 0;
            $this->new_item->obligated = 0;
            $this->new_item->impact_fee = 0;
            $this->new_item->total=0;
            $funding_sources->splice(null,null,$this->new_item);
        }

I'm not sure what to put in place of SOMETHING HERE that would mean that, when I splice on $this->new_item, it is an object like the other parts of the collection. If I can do that, I can more easily re-use the same view templates as when I don't do this summing. (I need to have both views.) What should I put in the SOMETHING HERE space? Or is there another way to get this object? I've poked around with variations of "new" and "newCollection," but I can't find the correct incantation. :)

Also, my plan would be to next go through the collection, using filters to get each sub-set of funding by fiscal years and adding them to the "funded," "spent," etc items in this new object. If there's an easier way to do all this, I wouldn't mind being pointed in the right direction. I feel like there SHOULD be an easier way to do this, but I don't yet know the right terms to find and implement it.

Thanks in advance!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.