php - 如何在 Slim Framework 中使用 Laravel illuminate 的数据库

标签 php mysql laravel model-view-controller slim

我使用的是 slim framework version 3。我使用过 Laravel 的 illuminate 数据库。在我的 Controller 中,当我写下面的查询时,它给我错误“找不到类数据库”我还指定了使用数据库;尽管在我的 Controller 中它不允许我编写这样的 mysql 查询。

$students = DB::table('students')->row();

这是我的 composer.json 文件内容

"require": {
        "slim/slim": "^3.0",
        "slim/twig-view": "^1.0",
        "vlucas/valitron": "^1.2",
        "slim/csrf": "^0.3.3",
        "slim/flash": "^0.1.0",
        "illuminate/database": "5.2.*",
        "illuminate/events": "5.2.*",
        "illuminate/cache": "5.2.*",
        "illuminate/filesystem": "5.2.*",
        "luracast/config": "2.*",
        "vlucas/phpdotenv": "~1.0",
        "league/flysystem": "~1.0",
        "illuminate/pagination": "5.2.*"
     },

我的供应商目录中有所有必要的文件。但是我想在我的 Controller 中使用数据库

这是我的 Controller 代码:-

<?php

namespace Controller;


use Slim\Views\Twig;
use Slim\Router;
use Slim\Flash\Messages as FlashMessages;
use Model\Student;
use DB;

final class StudentController
{
    private $view;
    private $router;
    private $flash;

    public function __construct(Twig $view, Router $router, FlashMessages $flash)
    {
        $this->view = $view;
        $this->router = $router;
        $this->flash = $flash;
    }

    public function listStudents($request, $response, $params)
    {   
        $students = DB::table('students')->get();
        return $this->view->render($response, 'students/list.twig', [
            'students' => Student::all(),
        ]);
    }
}

最佳答案

您必须更正使用的命名空间:

use \DB;

use Illuminate\Support\Facades\DB;

而不是使用数据库

您还可以查看 using database illuminate without Laravel 的教程

关于php - 如何在 Slim Framework 中使用 Laravel illuminate 的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44159615/

相关文章:

MySql 查找给定坐标的条目距离时出错

javascript - Laravel javascript 分页、打印、提交 View

javascript - 窗口.close();在 Laravel Controller 中

php - 图像文件为空

php - 为什么 PHP 允许您在构造函数上重载类型提示,但不允许在方法上重载类型提示?

mysql - 更改读取超时后仍然遇到 Mysql 2013 错误

MySQL时间戳/日期默认值

php - 带警告消息的 Laravel DB 查询

php - jQuery slider 值保存

php - PHP fatal error : Call to undefined method K2P_API_OCWRITER::cleanPost(). OpenCart