What do you mean by previous model. Do you have some code we can see?
Previous Model ?
Hello everyone,
I try to code a cumulative field directly updated in my model.
I found this link
https://laracasts.com/discuss/channels/eloquent/sum-two-fields-in-the-same-table
but don't understand the Best Answer, how can we get the previous model ? ordered by id? filtered / ordered by some more complex criteria?
See Best Answer of in the link
https://laracasts.com/discuss/channels/eloquent/sum-two-fields-in-the-same-table
@pc579 that does 2 queries as well
I have no code yet but let describe an example as simple as possible :
Model
- user_id
- area
- value
- value_sum_id
- value_sum_user
- value_sum_user_area
Logic
- value_sum_id = value (id) + value_sum_id (id-1)
- value_sum_user = value(id) + value_sum_user(previous id for this user)
- value_sum_user_area = value(id) + value_sum_user(previous id for this user & area)
Hope I'm clear and sorry to have written/saved my post in many times.
@pc579 I'm unsure why you need to check the previous user in the database. And what if you delete a record. Should it then be -2?
And is it just 2 rows and nothing else? Or all rows?
for user it can be an admin query, but you are right, user_id should be better a filter (scope) as written in my initial post.
I did not write the initial value but it was implicit that the sums start from 0.
Had you a look to the Best Answer of the provided link, how this code can work?
@pc579 ok so you get a user id and you need to sum it with the user that was created before them? If so, you can just do 2 queries, to get both rows
Ok, no problem with this, my question is only related to the provided link, anything I missed in the doc ... to get the variable $previousBill ?
So I assume you have the first row already?
$previous = Bill::where('user_id', $current->user_id)->where('id', '<', $current->id)->first();
Right but how it arrives in the creating function from the observer?
@pc579 that assumes that the first bill has just been set to be created (but not saved yet). In that case you should use the code from the other post. My code assumes it already exist
Is this just about understanding how the best answer works in the other thread? Cause I can explain that
Sorry, don't understand, when I try the previous code; I got undefined variable $previousBill !
@pc579 show your implementation of the code
@pc579 does it make more sense for you like this?
public function creating(Bill $bill)
{
$previousBill = Bill::where('property_id', $bill->property_id)->latest('id')->first();
if ($previousBill) {
$bill->bill_amount_left = $previousBill->bill_amount_left + $bill->amount;
}
}
@pc579 the event is documented here https://laravel.com/docs/9.x/eloquent#events-using-closures
And the setting a variable in the if() is just plain php. You can set and check a variable at the same time :)
This time I really learned st useful . Thank very MUCH.
@pc579 happy to help :)
The safest bet is 42.
@pc579 There is no 43, only 42.
Ok I learned st ...
<?php
namespace App\Observers;
use App\Models\Mouvement;
class MouvementObserver
{
/**
* Handle the Mouvement "created" event.
*
* @param \App\Models\Mouvement $mouvement
* @return void
*/
public function created(Mouvement $mouvement)
{
//
}
/**
* Handle the Mouvement "updated" event.
*
* @param \App\Models\Mouvement $mouvement
* @return void
*/
public function updated(Mouvement $mouvement)
{
dump("MouvmenetObserver updated");
}
public function updating(Mouvement $mouvement)
{
dump("MouvmenetObserver updating");
dump($mouvement);
// dump($previousMouvement); # -> undefinde
}
...
What else do you need?
@pc579 not sure how that can throw an error with $previousBill? That variable isn't used in the code
Right in my example it was previousMouvement ... some (undocumented) Laravel magic ! :-)
Please or to participate in this conversation.