Skip to content
Advertisement

Laravel tap into moment before query execution

I am using Laravel scopes to limit what the end user can see based on some provided input, however with my current use-case this is no longer acceptable.

Before applying additional queries, I would like to see what Eloquent method is being called, or at least see whether the ending SQL is performing a SELECT, INSERT or whatever before it is actually executed.

I can see that the Model is loading scopes within a constructor, which leads me to the conclusion that the actual query which would be executed without scopes is somewhere further down the line, however I can’t seem to find it.

Is there some point where I can insert scopes (or similar equivalent) to the point right before the actual query is executed?

EDIT: even more confusing is the fact that all() method doesn’t go inside Eloquent Builder…

Advertisement

Answer

What I wanted to do is not possible. Scopes are loaded and applied pretty early (before any Eloquent event is fired) upon model instantiation.

I had 2 ideas: one in this post, and this one; none of which worked. In the latter you can see what was the use case I needed, and how I ended up solving it.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement