首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel框架中的自定义函数原始查询

Laravel框架中的自定义函数原始查询
EN

Stack Overflow用户
提问于 2014-03-07 14:06:36
回答 1查看 1.1K关注 0票数 0

请告诉我在哪里可以在Laravel框架中添加自定义功能,或者在安装中缺少什么?我试着用这个函数

代码语言:javascript
复制
public function select($query, $bindings = array())
{
    return $this->run($query, $bindings, function($me, $query, $bindings)
    {
        if ($me->pretending()) return array();

        // For select statements, we'll simply execute the query and return an array
        // of the database result set. Each element in the array will be a single
        // row from the database table, and will either be an array or objects.
        $statement = $me->getPdo()->prepare($query);

        $statement->execute($me->prepareBindings($bindings));

        return $statement->fetchAll($me->getFetchMode());
    });
}

从教程http://fideloper.com/laravel-raw-queries,但我找不到修改我现有的Laravel框架的地方。

我需要运行一个查询,从3个表中进行内部连接,并收集数据并在网格中发布。我需要修改Laravel框架并创建我自己的函数。

请帮帮忙。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-07 15:03:50

是的,所以在你的控制器中,你有与路线相对应的方法。因此,选择与您的路由对应的方法,并在该方法中调用此函数。

例如,在HomeController.php中

代码语言:javascript
复制
Class HomeController extends BaseController {
    public function index() {
        $yourData = DB::raw('your query');
        // if you want to inject it in your view.
        return View::make('yourtemplatename', ['yourdata' => $yourData]);
    }
}

在你的文件中,routes.php

代码语言:javascript
复制
route::get('/', 'HomeController@index');

但有一种最漂亮的方式,可以用雄辩的口才来进行询问。检查文档,因为that.Your查询并不像它看上去的那样难,因为它是一个连接的林子。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22252282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档