php - Laravel 查询构建器返回空结果集

标签 php mysql laravel

我非常努力地理解为什么我在 Laravel Controller 中的代码和我使用的 SQL 语句返回空结果集,而正如您将看到的我的 PhpMyAdmin 页面的屏幕截图,创建了一个表,它包含的数据正如我在 HomeController.php 中请求的代码一样。以下是SQLPhpMyAdmin页面的代码和图片:

HomeController.php:

        $user_id = Auth::user()->id;
        $user_name = Auth::user()->name;
        $table_name = $user_name . '_' . $user_id;
        $return_value = 'The user\'s Table is now present : ' . $table_name;

        if (Schema::hasTable($table_name))
        {   
            $langs = DB::table($table_name)->get();
            return view('home', ['rvalue' => $langs]);
        }else{
            Schema::connection('mysql')->create($table_name, function ($table){
                $value = 'necro';
                $table->increments('id');
                $table->string('en')->default($value);
                $table->string('fr')->default($value);
                $table->string('sp')->default($value);
            });

            $langs = DB::table($table_name)->get();
            return view('home', ['rvalue' => $langs]);

        }



MySQL 命令检查数据库: enter image description here



PhpMyAdmin: enter image description here

您能帮我理解 HomeController.php 中的代码有什么问题吗?我使用 phpmyadmin 和 Utf8_General_ci 编码创建了数据库。

最佳答案

它返回一个空集,因为表实际上是空的。 phpadmin 屏幕截图显示了表的结构,但没有显示其内容。

 Schema::connection('mysql')->create($table_name, function ($table){
            $value = 'necro';
            $table->increments('id');
            $table->string('en')->default($value);
            $table->string('fr')->default($value);
            $table->string('sp')->default($value);
        });

        $langs = DB::table($table_name)->get();
        return view('home', ['rvalue' => $langs]);

在这段代码中,您只创建了一个表,但从未向其中添加任何数据。

关于php - Laravel 查询构建器返回空结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40340696/

相关文章:

javascript - WordPress:将图像从一种形式传递到另一种形式

laravel - 如何在 Laravel 中使用 Regex 路由前缀?

php - Laravel Command 参数返回键值对而不是值

php - 如何在 Laravel 5.2 中编辑 bootstrap css 文件

php - 丢失 php 作为文件类型 sublime 3

php - 为什么我的 PHP 上传脚本无法在 jquery Mobile 上运行?

php - 将 php 文件从 eclipse 发布到 apache htdocs

MySQL:整理查询 - 有任何副作用吗?

php - 从 CodeIgniter 中的核心 Controller 获取数据

MYSQL IN语句