How to pass additional parameters on the observer model events (and model custom events) of Laravel

reference documentation

for example:

class MyObserver
{
    public function saving(Model $model)
    {
        // $model
    }
}

this method is currently used to deal with it:

protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }
   

although it is said that the current problem can be solved, I hope to be able to modify it in a similar way:

public function saving(Model $model, argument1, argument2....)

ask for advice on how to achieve this thought, or some other more convenient method, thank you

Mar.08,2022

look at the source code should not work, only you can inject


refer to the following code to add attributes to Model. At this point, the custom properties are also passed to the observer.

<?php

class UserObserver {

    public function saving(User, $user) {
        echo $user->argument1;
        echo $user->argument2;
    }

}
Menu