Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 617
Ivica Pesovski Avatar asked Aug 31 '25 15:08

Ivica Pesovski


1 Answers

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 
like image 167
loic.lopez Avatar answered Sep 02 '25 06:09

loic.lopez