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