Laravel 8 - 找不到驱动程序:Illuminate\Database\QueryException 找不到驱动程序(SQL:select * 来自`list`)
Laravel 8 - Could not find driver : Illuminate\Database\QueryException could not find driver (SQL: select * from `list`)
我已经在我的 Linux Mint 20 上安装了 Laravel 8 用于我的个人实验,所以我是 Laravel 新版本的新手。我搜索了很多资源如何使用 CRUD 方法显示 table,因此 table 在网络中显示包含来自 MySQL 数据库
的数据
但是当我尝试用 CRUD 方法显示 table 时,它看起来像这样:
Illuminate\Database\QueryException
could not find driver (SQL: select * from list
)
在localhost:8000/home/tabel
我尝试通过修复 .env 文件、Controller 文件、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');
这是数据库和 table,我用它来显示来自 CRUD 方法的 tables ->
对于 MySQL 数据库,我使用 XAMPP
任何人都可以解释为什么这是错误并给我解决方案我应该怎么做才能修复这个问题?
只需为 PHP-MySQL 安装合适的驱动程序:
# default
sudo apt install php-mysql
# for specific version of php (e.g. php7.4)
sudo apt install php7.4-mysql
重新启动您的服务器:
# apache
sudo systemctl restart apache2
# nginx
sudo systemctl restart nginx
我已经在我的 Linux Mint 20 上安装了 Laravel 8 用于我的个人实验,所以我是 Laravel 新版本的新手。我搜索了很多资源如何使用 CRUD 方法显示 table,因此 table 在网络中显示包含来自 MySQL 数据库
的数据但是当我尝试用 CRUD 方法显示 table 时,它看起来像这样:
Illuminate\Database\QueryException could not find driver (SQL: select * from
list
)
在localhost:8000/home/tabel
我尝试通过修复 .env 文件、Controller 文件、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');
这是数据库和 table,我用它来显示来自 CRUD 方法的 tables ->
对于 MySQL 数据库,我使用 XAMPP
任何人都可以解释为什么这是错误并给我解决方案我应该怎么做才能修复这个问题?
只需为 PHP-MySQL 安装合适的驱动程序:
# default
sudo apt install php-mysql
# for specific version of php (e.g. php7.4)
sudo apt install php7.4-mysql
重新启动您的服务器:
# apache
sudo systemctl restart apache2
# nginx
sudo systemctl restart nginx