Skip to content
Advertisement

Get the $request object in Laravel’s model events (ex. created, updated etc.)

I am using Eloquent’s model events to do some actions after model has been created/updated/deleted. I’m refering to these:

class User extends Model 
{

    public static function boot()
    {
        parent::boot();

        self::created(function($model){
            // ... code here
        });

        self::updated(function($model){
            // ... code here
        });

        self::deleted(function($model){
            // ... code here
        });
    }

}

The question: I need to access the $request object in any of these. Is it possible? The scenario I am trying to do is to insert some values in another database table once the current model has been stored. The values are in the $request. And I don’t have access to the controller to do it manually there.

Advertisement

Answer

You can look at: Laravel access request object outside controller

There is request helper in laravel. You can use Request Object anywhere. For example

request()->field_name 
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement