Solucionado (ver solução)
Solucionado
(ver solução)
7
respostas

Conexão com o banco de dados

Estou tendo problemas em continuar no treinamento por causa deste erro, alguém pode dar um help?

(2/2) QueryException
could not find driver (SQL: select * from produtos)
in Connection.php (line 647)
at Connection->runQueryCallback('select * from produtos', array(), object(Closure))
in Connection.php (line 607)
at Connection->run('select * from produtos', array(), object(Closure))
in Connection.php (line 326)
at Connection->select('select * from produtos')
in DatabaseManager.php (line 327)
at DatabaseManager->__call('select', array('select * from produtos'))
in Facade.php (line 221)
at Facade::__callStatic('select', array('select * from produtos'))
in ProdutoController.php (line 9)
at ProdutoController->listaProdutos()
at call_user_func_array(array(object(ProdutoController), 'listaProdutos'), array())
in Controller.php (line 55)
at Controller->callAction('listaProdutos', array())
in ControllerDispatcher.php (line 44)
at ControllerDispatcher->dispatch(object(Route), object(ProdutoController), 'listaProdutos')
in Route.php (line 203)
at Route->runController()
in Route.php (line 160)
at Route->run()
in Router.php (line 574)
at Router->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in SubstituteBindings.php (line 41)
at SubstituteBindings->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in VerifyCsrfToken.php (line 65)
at VerifyCsrfToken->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ShareErrorsFromSession.php (line 49)
at ShareErrorsFromSession->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in StartSession.php (line 64)
at StartSession->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in AddQueuedCookiesToResponse.php (line 37)
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in EncryptCookies.php (line 59)
at EncryptCookies->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Router.php (line 576)
at Router->runRouteWithinStack(object(Route), object(Request))
in Router.php (line 535)
at Router->dispatchToRoute(object(Request))
in Router.php (line 513)
at Router->dispatch(object(Request))
in Kernel.php (line 176)
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ValidatePostSize.php (line 27)
at ValidatePostSize->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in CheckForMaintenanceMode.php (line 46)
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Kernel.php (line 151)
at Kernel->sendRequestThroughRouter(object(Request))
in Kernel.php (line 116)
at Kernel->handle(object(Request))
in index.php (line 53)
at require_once('/home/mateus/Documentos/ProjetsLaravel/estoque/public/index.php')
in server.php (line 21)
(1/2) PDOException
could not find driver
in Connector.php (line 68)
at PDO->__construct('mysql:host=127.0.0.1;port=3306;dbname=homestead', 'homestead', 'secret', array(0, 2, 0, false, false))
in Connector.php (line 68)
at Connector->createPdoConnection('mysql:host=127.0.0.1;port=3306;dbname=homestead', 'homestead', 'secret', array(0, 2, 0, false, false))
in Connector.php (line 44)
at Connector->createConnection('mysql:host=127.0.0.1;port=3306;dbname=homestead', array('driver' => 'mysql', 'host' => '127.0.0.1', 'port' => '3306', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'unix_socket' => '', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'name' => 'mysql'), array(0, 2, 0, false, false))
in MySqlConnector.php (line 24)
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => '127.0.0.1', 'port' => '3306', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'unix_socket' => '', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'name' => 'mysql'))
in ConnectionFactory.php (line 183)
at ConnectionFactory->Illuminate\Database\Connectors\{closure}()
at call_user_func(object(Closure))
in Connection.php (line 883)
at Connection->getPdo()
in Connection.php (line 904)
at Connection->getReadPdo()
in Connection.php (line 392)
at Connection->getPdoForSelect(true)
in Connection.php (line 318)
at Connection->Illuminate\Database\{closure}('select * from produtos', array())
in Connection.php (line 640)
at Connection->runQueryCallback('select * from produtos', array(), object(Closure))
in Connection.php (line 607)
at Connection->run('select * from produtos', array(), object(Closure))
in Connection.php (line 326)
at Connection->select('select * from produtos')
in DatabaseManager.php (line 327)
at DatabaseManager->__call('select', array('select * from produtos'))
in Facade.php (line 221)
at Facade::__callStatic('select', array('select * from produtos'))
in ProdutoController.php (line 9)
at ProdutoController->listaProdutos()
at call_user_func_array(array(object(ProdutoController), 'listaProdutos'), array())
in Controller.php (line 55)
at Controller->callAction('listaProdutos', array())
in ControllerDispatcher.php (line 44)
at ControllerDispatcher->dispatch(object(Route), object(ProdutoController), 'listaProdutos')
in Route.php (line 203)
at Route->runController()
in Route.php (line 160)
at Route->run()
in Router.php (line 574)
at Router->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in SubstituteBindings.php (line 41)
at SubstituteBindings->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in VerifyCsrfToken.php (line 65)
at VerifyCsrfToken->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ShareErrorsFromSession.php (line 49)
at ShareErrorsFromSession->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in StartSession.php (line 64)
at StartSession->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in AddQueuedCookiesToResponse.php (line 37)
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in EncryptCookies.php (line 59)
at EncryptCookies->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Router.php (line 576)
at Router->runRouteWithinStack(object(Route), object(Request))
in Router.php (line 535)
at Router->dispatchToRoute(object(Request))
in Router.php (line 513)
at Router->dispatch(object(Request))
in Kernel.php (line 176)
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ValidatePostSize.php (line 27)
at ValidatePostSize->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in CheckForMaintenanceMode.php (line 46)
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Kernel.php (line 151)
at Kernel->sendRequestThroughRouter(object(Request))
in Kernel.php (line 116)
at Kernel->handle(object(Request))
in index.php (line 53)
at require_once('/home/mateus/Documentos/ProjetsLaravel/estoque/public/index.php')
in server.php (line 21)

ProdutoController

<?php
namespace estoque\Http\Controllers;

use Illuminate\Support\Facades\DB;

class ProdutoController extends Controller{

    public function listaProdutos(){
        $produtos = DB::select('select * from produtos');
        dd($produtos);
    }
}

database.php

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'estoque'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', '12345'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
7 respostas

O problema é que, mesmo que você edite esse arquivo, o Laravel está configurado para seguir as configurações dentro do arquivo .env

Dá uma olhada nesse arquivo e coloque a suas credenciais de acesso lá.

As vezes o Laravel faz cache da configuração dentro do arquivo .env

Se isso acontecer com você, execute o comando abaixo no console na raiz do seu projeto

php artisan config:clear

Existe um outro tópico resolvido muito parecido com o seu, segue link abaixo

https://cursos.alura.com.br/forum/topico-conexao-mysql-com-laravel-5-0-43358

Fiz todas as alterações e o erro continua. Segue o código:

.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:e/GvCOEG3THPNVYoexrEQ3ylJNkSN3TEmu+vgMUZr3A=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=estoque
DB_USERNAME=root
DB_PASSWORD=12345

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

.env.example

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=estoque
DB_USERNAME=root
DB_PASSWORD=12345

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

database.php

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'estoque'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', '12345'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

ProdutoController.php

<?php

namespace estoque\Http\Controllers;
use Illuminate\Support\Facades\DB;

class ProdutoController extends Controller{
    public function listarProdutos(){
        $produtos = DB::select('select * from produtos');
        dd($produtos);
        return "<h3>Listagem de Produtos</h3>";
    }
}

Pelo que eu vejo, está faltando ativar as extensões do mysql no seu php.

Se estiver usando Windows, abra o arquivo php.ini na raiz da sua instalação PHP, procure pela linhas abaixo e tire o ';' do inicio da mesma conforme abaixo:

Linha original

;extension=php_pdo_mysql.dll
;extension=php_mysql.dll

Como deve ficar ( Sem o ';' no começo )

extension=php_pdo_mysql.dll
extension=php_mysql.dll

Se estiver no Ubuntu / Debian, execute o comando abaixo

sudo apt-get install php5-mysql

Tem que reiniciar o servidor / serviço pra funcionar

Nos de um retorno aqui com o resultado.

solução!

Problema resolvido.

Se você está usando o comando

 php artisan serve

tem que para e recomeçar.

No arquivo config/database.php a linha da conexão padrão está como abaixo?

'default' => env('DB_CONNECTION', 'mysql'),

Se tudo está ok, então não sei mais como tentar ajudar.

Qual era o problema André?

O problema era o driver mysql e o pdo_mysql do PHP que não estava instalado, instalei e funcionou tranquilamente.