Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SalmanMalik's avatar

How to get current executing Raw Query in Model Observer Methods in Laravel?

I'm making a approval system where someone initiates a request to either save/update/delete some data from database but they need to get approvals from specified persons. I've created all the models/jobs/notifications but there were some complexities to do this. I came up with an idea to store requested raw query in database but I couldn't figure out how to get only specific query which is executing for that model.

Custom trait used in model

Here I've created a trait which is booting and observing updating method (for now). What I want is to get the query which is executing and store it in my database. It'd be really appreciated if someone can help me out. :) Thank You.

0 likes
1 reply
Snapey's avatar

This would be a very problematic way of doing it. Better to do it before any database query is made. Create a draft version of the change within the controller, not try and intercept the model changes.

Please or to participate in this conversation.