Access Request Object in an Eloquent Model

I’m trying to use traits in Eloquent models to insert information about the request into an audit table. How can I access the Request object? I have tried using:

use Psr\Http\Message\ServerRequestInterface as Request;

class Audit extends Model {

public function __construct(Request $request)
    $this->ip_address = $request->getAttribute('ip_address');
    $this->user_agent = $request->getHeaderLine('User-Agent');


The Audit model is only called in the Trait which i attach to other Models.

It’s not working. I always get this error:

Too few arguments to function App\Appic\Models\Audit::__construct(), 0 passed

How can I achieve this?

Are you sure that you want to mix the controller / infrastructure layer (http) with the domain and data access layer? I would try to keep it separated by passing the data from the request (not the request object) to the domain and from the domain to your entities.


Thanks for your reply. I am not sure I understand what you are saying sorry. I am not a very advanced developer. Would you mind showing me with some code on what you mean. My front end is a vue application and so I authorize the api requests using a JWT. I wish to take the JWT and get some information from it so I can log who did what in the DB.

Thanks for your patience and help.


I found another thread before mine where you answered a guy who I think has the same issue as mine. I understand now what you mean. But the solution you suggested seems to require that I pass the request data to the model everytime I use them.Would it better that the data is passed automatically to the model whenever it is used? How can I auto pass the data to themodel?

Thanks again.


As far as I know in Laravel all these details are handled behind the Auth facade. For your Slim app you need a more explicit solution that fits better into the dependency injection / container concept.

Inspired by the Laravel Auth concept and your specific requirements I would try this container friendly approach:

  • Create a DTO or a Value Object that holds the data from the current logged in user, e.g. UserData
  • Create a Auth class that holds the current user data instance (setUser, getUser)
  • Create a container definiton for the Auth::class
  • Create a UserAuthMiddleware that sets the information coming from the request (e.g. JWT details) fills the UserData instance and the sets it into the Auth class. $this->auth->setUser($user);
  • Now you can autowire the Auth class instance where you need it via dependency injection.

Hi Odan,

Thank you very much. I did suspect already that I would have to use middleware. Thanks for confirming it.