简体   繁体   中英

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...

I think you could throw an error, like this:

throw new Exception()

And then use the debug html output. It should have a tab, that shows the last queries. Not the most beautiful idea, but maybe it works.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM