php - 如何在 PHP Laravel Lumen 中建立与数据库的连接?

标签 php database laravel lumen lumen-5.2

我正在尝试通过 localhost 运行基于 Lumen 的查询。我不知道如何正确调用正确的数据库名称。

编辑:我收到以下错误的原因是因为我的 .env 我的项目文件中的数据库名称。 我的 .env 文件中的 DB_DATABASE=mydbschemaname 行需要有我的数据库名称,但我如何找到它?我到处都找不到。

database error

我的代码如下,routes.php位于app->Http:

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

UserController.php 在 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 在 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 在 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');
    }
}

当然,我有 database.php 位于 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),
        ],

    ],

];

最后,这是我的 .env 文件,它位于应用程序文件夹之外,在根项目文件夹中:

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

我假设这是我必须更改其中的 DB_DATABASE 名称的最后一个文件 (.env),但我不完全确定。任何帮助将不胜感激。

仅供引用:这里的最终结果是能够连接到数据库并显示我在用户表中添加的用户的记录。

最佳答案

你确定你有模式名称为“数据库”的数据库吗?

您需要在数据库设置后更改 DB_ 属性。

例如

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=mydbschemaname
DB_USERNAME=dbadmin
DB_PASSWORD=myverysecretpassword

关于php - 如何在 PHP Laravel Lumen 中建立与数据库的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38988996/

相关文章:

php - 如何取消引用构造函数?

mysql - 对大量数据使用像JSON_EXTRACT这样的SQL函数好不好

Laravel 9 - 如何防止在用户登录并点击浏览器后退按钮后显示登录页面

php - 找不到类 'Jenssegers\Mongodb\MongodbServiceProvider'

php - 传递表单值并移动到另一个页面

php - 如何按特定月份/年份选择记录

php - Linux sed 命令替换字符串不起作用

mysql - 在 MySQL 中,如何对单个选择使用两次表引用?

sql-server - SQL Server 二进制文件存储

php - Laravel:在多对多关系中获取不相关的项目