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

AULA 02 ERRO AO EXIBIR DADOS DO BANCO DE DADOS

Por favor me ajudem, não consigo listar os produtos na pagina, vi os foruns abertos pra essa aula e mesmo assim não deu certo

Meu ProdutoController.php está assim:

<?php namespace estoque\Http\Controllers;

use Illuminate\Support\Facades\DB;

class ProdutoController extends Controller {

    public function lista(){

        $produtos = DB::select('select * from produtos');

        return '<h1>Listagem de produtos com Laravel</h1>';
    }
}

Meu database.php está assim

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_CLASS,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

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

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'estoque_laravel'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

];

Mais não consigo listar os produtos na pagina, pois aparece o erro abaixo:

PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)

in Connector.php line 55 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 55 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', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, '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', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, 'name' => 'mysql')) in ConnectionFactory.php line 61 at ConnectionFactory->Illuminate\Database\Connectors{closure}() at call_user_func(object(Closure)) in Connection.php line 908 at Connection->getPdo() in Connection.php line 776 at Connection->reconnectIfMissingConnection() in Connection.php line 661 at Connection->run('select from produtos', array(), object(Closure)) in Connection.php line 342 at Connection->select('select from produtos') at call_user_func_array(array(object(MySqlConnection), 'select'), array('select * from produtos')) in DatabaseManager.php line 317 at DatabaseManager->call('select', array('select from produtos')) in Facade.php line 217 at DatabaseManager->select('select from produtos') in Facade.php line 217 at Facade::__callStatic('select', array('select from produtos')) in ProdutoController.php line 9 at DB::select('select from produtos') in ProdutoController.php line 9 at ProdutoController->lista() at call_user_func_array(array(object(ProdutoController), 'lista'), array()) in Controller.php line 80 at Controller->callAction('lista', array()) in ControllerDispatcher.php line 146 at ControllerDispatcher->call(object(ProdutoController), object(Route), 'lista') in ControllerDispatcher.php line 94 at ControllerDispatcher->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96 at ControllerDispatcher->callWithinStack(object(ProdutoController), object(Route), object(Request), 'lista') in ControllerDispatcher.php line 54 at ControllerDispatcher->dispatch(object(Route), object(Request), 'estoque\Http\Controllers\ProdutoController', 'lista') in Route.php line 174 at Route->runController(object(Request)) in Route.php line 140 at Route->run(object(Request)) in Router.php line 724 at Router->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing{closure}(object(Request)) in VerifyCsrfToken.php line 64 at VerifyCsrfToken->handle(object(Request), object(Closure)) at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in ShareErrorsFromSession.php line 49 at ShareErrorsFromSession->handle(object(Request), object(Closure)) at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in StartSession.php line 62 at StartSession->handle(object(Request), object(Closure)) at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37 at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) in EncryptCookies.php line 59 at EncryptCookies->handle(object(Request), object(Closure)) at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 at Pipeline->then(object(Closure)) in Router.php line 726 at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699 at Router->dispatchToRoute(object(Request)) in Router.php line 675 at Router->dispatch(object(Request)) in Kernel.php line 246 at Kernel->Illuminate\Foundation\Http{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 at CheckForMaintenanceMode->handle(object(Request), object(Closure)) at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 at Pipeline->then(object(Closure)) in Kernel.php line 132 at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99 at Kernel->handle(object(Request)) in index.php line 54

5 respostas

bi seu projeto tem um arquivo .env e nele deve estar com a configuração default do laravel, tanto que seu dbname esta o default que vem nesse arquivo dbname=homestead

Certo, qual alterações devo fazer? E como?

solução!

Você pode deletar ou passar toda as configurações do bd para ele

Alan, passei as informações para o arquivo .env e o erro não aparceu mais, porém não lista os produtos. Aparece apenas o titulo Listagem de produtos com Laravel, e abaixo nada aprece.

Obrigado pela força, se puder me ajudar onde estou errando agora agradeço.

Pronto amigo Alana, obrigado pela as ajudas, deu tudo certo agora. Havia feito o arquivo ProdutoController errado, deu certo agora com o seguinte codigo.

<?php namespace estoque\Http\Controllers;
use Illuminate\Support\Facades\DB;
class ProdutoController extends Controller {
    public function lista(){
        $produtos = DB::select ('select * from produtos');
        dd($produtos);
        return '<h1>Listagem de produtos com Laravel</h1>';
    }
}

Vlw.