简体   繁体   English

Laravel 进入查询执行前的时刻

[英]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.我正在使用 Laravel 范围来限制最终用户可以根据一些提供的输入看到的内容,但是对于我当前的用例,这不再可以接受。

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.在应用其他查询之前,我想看看正在调用什么 Eloquent 方法,或者至少看看结尾的 SQL 是否正在执行SELECTINSERT或其他任何实际执行之前。

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.我可以看到Model在构造函数中加载作用域,这使我得出结论,将在没有作用域的情况下执行的实际查询在更远的地方,但是我似乎找不到它。

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...编辑:更令人困惑的是all()方法在 Eloquent Builder 中没有 go ......

I think you could throw an error, like this:我认为你可能会抛出一个错误,如下所示:

throw new Exception()

And then use the debug html output.然后使用调试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.范围在 model 实例化时很早就被加载和应用(在任何 Eloquent 事件被触发之前)。

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.在后者中,您可以看到我需要的用例是什么,以及我最终是如何解决它的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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