Skip to content
Advertisement

Configuration observer in Laravel

I am beginner in Laravel. I use in my project Laravel 8.

I have this code:

  1. Controller
JavaScript
  1. Model
JavaScript
  1. Observer
JavaScript
  1. ServiceProvider
JavaScript

I have 2 questions / problems:

  1. How can I disable following in the controller (index method)? I only need to record the moment when someone opens one record for editing, and does not list all the records in the list

  2. I have model Action:

JavaScript

I need save to this model information about user ip, user_agent itp (user is logged). How can I make it?

Advertisement

Answer

As you’ve found, the “retrieved” method on the observer is called when you load the model instance, whether you load one or many (if you load many, it is called once for each model loaded).

You can suppress events being fired (and, having tested it, this includes both Events and Observers) by wrapping it in a callback function using the ::withoutEvents() static method.

So (using code from one of my sites) if I use :

JavaScript

then the GameObserver will be called 11 times (because there are 11 models which are loaded). But if I wrap it in the ::withoutEvents method like so :

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement