PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How to detect change in specific column in laravel model

 April 19, 2022     laravel, php     No comments   

Issue

I have a table structure like this :

subscribers with attributes: id, status

I want to have a way to check for the column status : If the status change to 'unsubscribed' then i want to auto detect thats change and apply do something with that.

The workflow is like this :

Anywhere in the code call $subscriber->status = 'unsubscribed'; I want to detect that and do something with that.

Thanks for reading.

I had use isDirty of laravel but that had to write after every $subscriber->status = 'unsubscribed'; so its did not do the work.


Solution

Add the following code to your model:

protected static function booted()
{
    static::updated(function (Subscriber $subscriber) {
        if ($subscriber->wasChanged('status')) {
            // Execute your code
        }
    });
}

Alternatively, you can use an Observer.



Answered By - Jonas
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing