Pup In The Tree
Pup In The Tree

Reputation: 199

Laravel 8 - Could not find driver : Illuminate\Database\QueryException could not find driver (SQL: select * from `list`)

I have installed Laravel 8 on my Linux Mint 20 for my personal experiment so I'm new on Laravel's new version. I've searching many source how to show tables with CRUD method so the table is shown in the web with contains data from MySQL database

But when i tried to show table with CRUD method, it appeared like this :

Illuminate\Database\QueryException could not find driver (SQL: select * from list)

in localhost:8000/home/tabel

I tried to solving this problem from repairing .env file, Controller file, blade file, and the web.php to be correct but it still error.

And this is my configuration file which i've change like this :

.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>

and then 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');

And this is database and table which i use to show tables from CRUD method -> enter image description here

For MySQL database i use XAMPP

Can anyone explain why this is error and give me solution what should I do to repair this?

Upvotes: 12

Views: 45682

Answers (3)

Genuka Manthila
Genuka Manthila

Reputation: 21

Clear Cache: Sometimes, cache issues can cause unexpected errors. Run the following commands in your Laravel project's root directory:

php artisan config:clear

and

php artisan cache:clear

Upvotes: 1

Gajen Dissanayake
Gajen Dissanayake

Reputation: 103

This is for the users who are using shared hosting where SSH facility may not be available.

Go to your Cpanel PHP settings, and make sure the following features (especially PHP 8.0 or above) are enabled.

mysqli pdo mysql

Because laravel's "DB" and "Models" in controllers need these features enabled. Otherwise the controller will raise the DRIVER NOT FOUND exception.

Upvotes: 0

boolfalse
boolfalse

Reputation: 2131

Just install appropriate driver for PHP-MySQL:

# default
sudo apt install php-mysql
# for specific version of php (e.g. php7.4)
sudo apt install php7.4-mysql

Restart your server:

# apache
sudo systemctl restart apache2
# nginx
sudo systemctl restart nginx

Upvotes: 35

Related Questions