请告诉我在 Laravel 框架中哪里可以添加自定义功能,或者安装中是否缺少某些内容? 我正在尝试使用该功能
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 框架中进行修改并创建自己的函数。
请帮忙。 谢谢。
最佳答案
是的,所以在你的 Controller 中你有与路由相对应的方法。 因此,选择与您的路线相对应的方法并在该方法中调用此函数。
例如在您的 HomeController.php 中
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中
route::get('/', 'HomeController@index');
但是有一种最漂亮的方法可以使用 Eloquent 进行查询。 检查文档。您的查询并不像看起来那么困难,它是一个连接森林。
关于php - Laravel 框架中的自定义函数原始查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22252282/