You could maybe use something like this. Keep in mind I haven't tested it, but it should be close. Essentially, you need to find all parents of a record, then update each parent by the specified amount, and then decrement the child by the amount * number of parents. At least, that's how I understood what you're trying to do.
public function updateAmount($amount)
{
$allParents = array();
$currentParent = $this->parent();
while($currentParent)
{
$newParent = $currentParent->parent();
if($newParent)
{
array_push($allParents, $newParent);
$currentParent = $newParent;
}
else
{
$currentParent = null;
}
}
if(count($allParents) > 0)
{
$amountPerParent = 5;
foreach ($parent in $allParents)
{
$parent->increment('amount', $amountPerParent);
}
$this->increment('amount', $amount - count($allParents)*$amountPerParent);
}
else
{
$this->increment('amount', $amount);
}
}
Edit: You would put this in your Contribution model. Then from your controller, you would simply call something like:
public function controllerAction($id)
{
$contribution = App::make('Contribution')->findOrFail($id);
$contribution->updateAmount(Input::get('amount'));
return Response::make('success', 200);
}
Thank you lookitsatravis, I am going to try it out and see. I appreciate the effort
lookitsatravis said:
You could maybe use something like this. Keep in mind I haven't tested it, but it should be close. Essentially, you need to find all parents of a record, then update each parent by the specified amount, and then decrement the child by the amount * number of parents. At least, that's how I understood what you're trying to do.
public function updateAmount($amount) { $allParents = array(); $currentParent = $this->parent(); while($currentParent) { $newParent = $currentParent->parent(); if($newParent) { array_push($allParents, $newParent); $currentParent = $newParent; } else { $currentParent = null; } } if(count($allParents) > 0) { $amountPerParent = 5; foreach ($parent in $allParents) { $parent->increment('amount', $amountPerParent); } $this->increment('amount', $amount - count($allParents)*$amountPerParent); } else { $this->increment('amount', $amount); } }
Edit: You would put this in your Contribution model. Then from your controller, you would simply call something like:
public function controllerAction($id) { $contribution = App::make('Contribution')->findOrFail($id); $contribution->updateAmount(Input::get('amount')); return Response::make('success', 200); }
I implemented your suggested solution and from the look of the code, you understood my issue. I however received an error
Call to undefined method Illuminate\Database\Query\Builder::parent()
The above error pointed to the line below:
$newParent = $currentParent->parent();
I also think that the following lines should have decrement instead of increment:
$this->increment('amount', $amount - count($allParents)*$amountPerParent);
}
else
{
$this->increment('amount', $amount);
}
I'm sure you've figured this out by now, but you probably need to change the lines to look like this:
line 4:
$currentParent = $this->parent()->first();
and then in the while loop
$newParent = $currentParent->parent()->first();
Basically, the example I wrote wasn't actually getting the record from the DB. Only building the query. ->first() solves that. As for the increments needing to be decrement...it shouldn't be. Take this example:
Say we have $20 and 3 parents to add $5 to. We don't want to do increment(20), then figure out the parent thing, and then do decrement(15). We figure out the parent thing first, then we just increment the difference. So, we wind up doing increment(20 - (3*5)) which gives us an increment of 5. In only one DB update. Make sense? Plus, if it wound up being a negative number, it would still work. 100 + -5 is 95 even if increment was called.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community