Skip to main content

Unsubscribing an observer while running create, update or delete operations on modal in Laravel

Today I was trying to copy some data from one application to another application. The Data has some nested relationships so I created a custom feature to fetch the data using Apis and insert them one by one into my destination application.

My problem while inserting the data was, there was an observer is set to File.php (for example) model that was running run logic to upload the incoming file to AWS s3 Cloude storage. But I am not really uploading anything while inserting the data. So, I want to stop the Observer from running the functionality while I was running my custom feature.

While looking for some solution to my problem I came to a solution that I would like to share with you.

File::unsetEventDispatcher();

I used this function call before running the logic only once and my observer stopped executing the code on Create and Update model event.

You can also use the same code to stop the other event from executing while running some logic.

You can get all the event displachers and filter the displachers you want to dispatch while running your logic.

$dispatcher = YourModel::getEventDispatcher();

Then you can update the $displacher array and set the Event displachers again:

YourModel::setEventDispatcher($dispatcher);

I hope this will help you to solve the problem and give you some new thoughts about playing with events.

Comments

Popular posts from this blog

The Great Architectures of Bijawar, Chhatarpur, M.P.

Bijawar, Temple Janki Niwash Bijawar, Temple Janki Niwash   Maharaja Palace of Bijawar - A very royal palace of the Great Maharaja Ji of Bijawar. The palace has a great eye caching architecture. It stands tall with its mirror image on a big Talab in front of it. The palace is become very old and loosing its age at a rapid pace. Unfortunately its to late to restore the Bijawar palace infrastructure but one should create Bijawar palace replica or model to preserve its beauty and architecture. This photo of the palace is the first photo taken and publish on the web. That time there was no good cameras available but as said Old is Gold.

Python Program To Find the Modulus with using modulus operator

  def cal_mod(dividend, divisor): # check the max divisor power of the dividend dividend_power = 1 while dividend > (divisor ** dividend_power) : dividend_power += 1 # reduce the dividend to the divisor_power + 1 # if dividend_power is greater then 2 if dividend_power > 2: while dividend > (divisor ** 2): dividend = cal_mod(dividend, (divisor ** (dividend_power - 1) )) dividend_power -= 1 while dividend >= divisor: dividend = dividend - divisor return dividend result = cal_mod(45645664566465456456878756765675699788866264623874682464, 16) print(result)

Get Google Calender's Event Notifications Or Reminder Notifications On Windows

Hi Friends, I was struggling for some days to get better notifications for google calender's events or reminders that I have set for myself but I was able to only get the browser notifications that was not quite working for me. I am not satisfied the way those browser notifications work for any calendar event or alert. The cons of browser notifications are: 1. They can't be snooze 2. They didn't sustain for long time, they just appears for 1 or 2 min maximum. 3. They are not very attractive. They can be missed easily. For these reasons the browser notifications are not a good solutions for Calendar notifications. So I searched on internet for any solutions that can provide me to get better notification for my google calendar events. By change today I get a window update today and I come to know about the Mail  app on  Window 10. This simple mail client app asked me to set up account with Google account and then it synced all my mails and calendars from my google a...