2

I'm trying to run a Lumen based query through localhost. I don't know how to properly call the right database name.

EDIT: The reason I'm getting the error below is because of the name of the db name in my .env file in my project. The line DB_DATABASE=mydbschemaname in my .env file needs to have my database name, but how do I find that out? I can't find it anywhere.

database error

The code I have is as follows, routes.php located in app->Http:

$app->get('/records', 'UserController@index');

UserController.php in app->Http->Controllers:

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller
{

    public function index() {
        $users = User::all();
        return response()->json($users);
    }
}

User.php in app->Http:

namespace App;

use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class User extends Model implements
    AuthenticatableContract,
    AuthorizableContract
{
    use Authenticatable, Authorizable;

    protected $fillable = [
        'name', 'email',
    ];

    protected $hidden = [
        'password',
    ];
}

[date]_create_users_table.php in app->database->migrations:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 200);
            $table->string('email', 200)->unique();
            $table->string('password', 200);
            $table->timestamps();
        });

        DB::table('users')->insert(
            ['id' => 1, 'name' => 'example', 'email' => 'example@example.com', 'password' => 'thisisthepassword', 'updated_at' => '2015-10-15 01:23:45', 'created_at' => '2015-10-15 01:23:45']
        );
    }

    public function down()
    {
        Schema::drop('users');
    }
}

Then of course, I have the database.php located in app->vendor->laravel->lumen-framework->config:

<?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' => [

        'testing' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
        ],

        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => env('DB_DATABASE', base_path('database/database.sqlite')),
            'prefix'   => env('DB_PREFIX', ''),
        ],

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'port'      => env('DB_PORT', 3306),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => env('DB_CHARSET', 'utf8'),
            'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
            'prefix'    => env('DB_PREFIX', ''),
            'timezone'  => env('DB_TIMEZONE', '+00:00'),
            'strict'    => env('DB_STRICT_MODE', false),
        ],

        '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'  => env('DB_CHARSET', 'utf8'),
            'prefix'   => env('DB_PREFIX', ''),
            'schema'   => env('DB_SCHEMA', 'public'),
        ],

        'sqlsrv' => [
            'driver'   => 'sqlsrv',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => env('DB_CHARSET', 'utf8'),
            'prefix'   => env('DB_PREFIX', ''),
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | 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' => env('REDIS_CLUSTER', false),

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

    ],

];

Finally, here's my .env file located outside the app folder, in the root project folder:

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomKey!!!

APP_LOCALE=en
APP_FALLBACK_LOCALE=en

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=memcached
SESSION_DRIVER=memcached
QUEUE_DRIVER=database

I'm assuming it's this last file (.env) that I have to change the DB_DATABASE name in, but I'm not entirely sure. Any help would be appreciated.

FYI: The end result here is to be able to connect to the database and display that record for user that I'm adding in the user table.

LatentDenis
  • 2,839
  • 12
  • 48
  • 99
  • Yes, you need to set the connection information to your database in the `.env` file. – patricus Aug 17 '16 at 06:41
  • @patricus I understand that. I don't know the name of my db, therefore I can't connect. Is there a way to find this out? – LatentDenis Aug 17 '16 at 12:53
  • You are responsible for creating the database. Once you create the database, update your `.env` file with the connection information (e.g. the name of the database you created), and then run the migrations (`php artisan migrate`) to create the tables inside the database. – patricus Aug 17 '16 at 14:56
  • @patricus To create the database, will I need to use any mySQL database software like mySQL management server? or can I do this through GIT bash command line or composer. This is where I'm stuck. I don't know how to go about making the database. – LatentDenis Aug 17 '16 at 15:04
  • In that case, I would do a little bit more searching. There's a ton of information already out there on how to do that. – patricus Aug 17 '16 at 15:23

1 Answers1

1

Are you sure you have database with schema name 'database'?

You need to change the DB_ properties as follow as your database is set.

E.g.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=mydbschemaname
DB_USERNAME=dbadmin
DB_PASSWORD=myverysecretpassword
miikes
  • 964
  • 7
  • 13
  • Unfortunately, this doesn't help at all, as I'm confused with where/how to find the name of the database. This is ultimately what's holding me back. – LatentDenis Aug 17 '16 at 12:51
  • I put only example. You need to configure your own database. Read about databases, MySQL (or other), create your own, named it and put the name into your `.env` config file. – miikes Aug 17 '16 at 15:09
  • My question is, can I create the DB by just going through GIT bash command line? or do I need to use a software like mySQL Management studio to accomplish that? – LatentDenis Aug 17 '16 at 15:13
  • You can touch database.sqlite file and push it via git. Remember to change `DB_CONNECTION=sqlite`. – miikes Aug 17 '16 at 19:36