我在我的Linux Mint 20上安装了Laravel 8用于个人实验,所以我对Laravel的新版本不熟悉。我搜索了许多资源,以了解如何使用CRUD方法显示包含来自MySQL数据库的数据的表格。
但是当我尝试使用CRUD方法显示表格时,出现了以下错误:
Illuminate\Database\QueryException could not find driver (SQL: select * from
list
)
在localhost:8000/home/tabel中。
我尝试从修复.env文件、控制器文件、blade文件和web.php文件来解决这个问题,但仍然出现错误。
这是我更改的配置文件:
.env
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=people
DB_USERNAME=root
DB_PASSWORD=
homeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class homeController extends Controller
{
public function home()
{
return "home";
}
public function tabel()
{
$tabelku = DB::table('list')->get();
return view('tabel', ['people' => $tabelku]);
}
}
tabel.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Table</title>
</head>
<body>
<div align="center">
<table border = "1">
<tr>
<th>No</th>
<th>Name</th>
<th>Age</th>
<th>Hobby</th>
</tr>
@foreach($tabelku as $t)
<tr>
<th>{{$t->no}}</th>
<th>{{$t->name}}</th>
<th>{{$t->age}}</th>
<th>{{$t->hobby}}</th>
</tr>
@endforeach
</table>
</div>
</body>
</html>
然后是 web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/hello', function () {
return 'Halo Dunia';
});
Route::get('/home','homeController@home');
Route::get('/home/tabel','homeController@tabel');
我使用XAMPP作为MySQL数据库。
可以有人解释一下为什么出现这个错误,并给出修复方法吗?
php.ini
中启用它。 - darthsoup