简体   繁体   English

我应该在Laravel 4应用程序中添加原始SQL查询的位置?

[英]Where should I add my raw SQL queries in my Laravel 4 app?

I'm slightly confused and need a bit of guidance if someone wouldn't mind. 我有点困惑,如果有人不介意,需要一些指导。

Just a bit of background, my application is a website containing a simple survey (a list of questions with possible responses retrieved from the database) Thus far, I have created the models for the questions and responses tables and have defined the relationships within these classes - ie, a question can have multiple possible responses whereas a response belongs to one question. 只是一点背景,我的应用程序是一个包含简单调查的网站(从数据库中检索可能响应的问题列表)到目前为止,我已经为问题和响应表创建了模型,并定义了这些类中的关系 - 即,问题可以有多个可能的响应,而响应属于一个问题。 I've used Eloquent to handle the retrieval of the data which works fine. 我使用Eloquent处理数据的检索工作正常。

Now, I need to run a query using the query builder to retrieve the posted data because of some complex left joins that I cannot do using Eloquent, but I'm uncertain where to put these - do I put the query inside of a new model, or run it from my controller like I have when using Eloquent? 现在,我需要使用查询构建器运行查询以检索发布的数据,因为我使用Eloquent无法做到的一些复杂的左连接,但是我不知道在哪里放这些 - 我是否将查询放在新模型中,或者像我在使用Eloquent时那样从我的控制器运行它?

该代码应该包含在模型的方法中。所以如果你需要为用户做一个疯狂的连接..它将是$user->getSomeCrazyJoinedData() ...如果你想要你也可以使用查询范围将参数传递给它。

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

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