Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pc579's avatar
Level 1

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?

0 likes
27 replies
Sinnbeck's avatar

What do you mean by previous model. Do you have some code we can see?

1 like
pc579's avatar
Level 1

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.

Sinnbeck's avatar

@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?

1 like
pc579's avatar
Level 1

@Sinnbeck

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.

pc579's avatar
Level 1

@Sinnbeck

Had you a look to the Best Answer of the provided link, how this code can work?

Sinnbeck's avatar

@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

pc579's avatar
Level 1

@Sinnbeck

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 ?

Sinnbeck's avatar

So I assume you have the first row already?

$previous = Bill::where('user_id', $current->user_id)->where('id', '<', $current->id)->first();
1 like
pc579's avatar
Level 1

@Sinnbeck

Right but how it arrives in the creating function from the observer?

Sinnbeck's avatar

@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

Sinnbeck's avatar

Is this just about understanding how the best answer works in the other thread? Cause I can explain that

1 like
pc579's avatar
Level 1

@Sinnbeck

Sorry, don't understand, when I try the previous code; I got undefined variable $previousBill !

Sinnbeck's avatar

@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;
		}
    }
1 like
pc579's avatar
Level 1

@Sinnbeck

got it

the code was only partial and I though of a (undocumented) Laravel magic.

pc579's avatar
Level 1

@sinnbeck @tray2

Thanks for you first reply but my post was not completed, it's now.

Hope you can understand / help ... without 43 :-)

pc579's avatar
Level 1

@sinnbeck

<?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?

Sinnbeck's avatar

@pc579 not sure how that can throw an error with $previousBill? That variable isn't used in the code

1 like
pc579's avatar
Level 1

@Sinnbeck

Right in my example it was previousMouvement ... some (undocumented) Laravel magic ! :-)

Please or to participate in this conversation.