如何在PHP Laravel Lumen中建立与数据库的连接?
我正试图通过如何在PHP Laravel Lumen中建立与数据库的连接?,php,database,laravel,lumen,lumen-5.2,Php,Database,Laravel,Lumen,Lumen 5.2,我正试图通过localhost运行基于流明的查询。我不知道如何正确地调用正确的数据库名称 编辑:我出现以下错误的原因是因为我的项目中的.env文件中的db名称。我的文件中的行db\u DATABASE=mydbschemaname。env文件需要有我的数据库名称,但我如何找到它?我到处都找不到 我的代码如下,routes.php位于app->Http中: $app->get('/records', 'UserController@index'); namespace App; use
localhost
运行基于流明的查询。我不知道如何正确地调用正确的数据库名称
编辑:我出现以下错误的原因是因为我的项目中的.env
文件中的db名称。我的文件中的行db\u DATABASE=mydbschemaname
。env
文件需要有我的数据库名称,但我如何找到它?我到处都找不到
我的代码如下,routes.php
位于app->Http中:
$app->get('/records', 'UserController@index');
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',
];
}
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:
$app->get('/records', 'UserController@index');
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]\u在app->database->migrations中创建用户\u table.php
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');
}
}
当然,我在app->vendor->laravel->lumen framework->config中有database.php
:
<?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
),我必须在其中更改DB\u数据库的名称,但我不能完全确定。任何帮助都将不胜感激
仅供参考:这里的最终结果是能够连接到数据库,并为我正在用户表中添加的用户显示该记录。您确定有架构名为“database”的数据库吗
在设置数据库时,您需要按如下所示更改DB_uu属性
例如
是的,您需要在.env
文件中设置与数据库的连接信息。@patricus我理解。我不知道数据库的名称,因此无法连接。有没有办法找到答案?你负责创建数据库。创建数据库后,使用连接信息(例如,您创建的数据库的名称)更新.env
文件,然后运行迁移(php artisan migrate
)在数据库内创建表。@patricus创建数据库,我是否需要使用mySQL数据库软件,如mySQL管理服务器?或者我可以通过gitbash命令行或composer来实现这一点。这就是我被困的地方。我不知道如何制作数据库。如果是这样的话,我会做更多的搜索。关于如何做到这一点,已经有大量的信息。不幸的是,这根本没有帮助,因为我不知道在哪里/如何找到数据库的名称。这就是最终阻碍我前进的原因。我只举了一个例子。您需要配置自己的数据库。阅读有关数据库、MySQL(或其他)的内容,创建自己的数据库,将其命名并将其放入.env
配置文件中。我的问题是,我可以通过GIT bash命令行创建数据库吗?或者我需要使用mySQL Management studio这样的软件来实现这一点吗?您可以触摸database.sqlite文件并通过git推送它。请记住更改DB\u CONNECTION=sqlite
。