【发布时间】:2020-11-02 16:20:27
【问题描述】:
我是 Laravel 框架和 php 的新手,我开始做基本的 Laravel 项目并在尝试进行迁移时收到他的错误消息(并且不更改文件中的任何内容,只需打开它并显示错误):避免使用静态访问到方法“up”中的类“\Illuminate\Support\Facades\Schema”
我的代码:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\ServiceProvider;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->mediumText('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
每个迁移类中的每个方法都出现相同的错误。
数据库.php:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'llsapp'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '123456'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
.env:
APP_NAME=LLSAPP
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=llsapp
DB_USERNAME="root"
DB_PASSWORD="123456"
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=
【问题讨论】:
-
试试
use Schema; -
嗨。看起来您将此代码放在错误的位置。这不应该是命名空间 App\Providers 并且不应该是
useServiceProvider 类。要创建迁移,请转到命令行(也许最好在宅基地)并运行php artisan make:migration create_post_table --create='posts'。然后通过php artisan migrate命令行运行迁移。 -
忘了说,迁移文件会出现在你的
/database/migrations/... -
使用模式对我不起作用。我删除了这 2 行使用 ServiceProvider 和命名空间,但仍然得到相同的错误,并且在运行 php artisan migrate 后我得到: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = llsapp and table_name = migrations) 在 Connector.php 第 68 行: SQLSTATE[HY000] [1045] 用户 'root'@'localhost' 的访问被拒绝(使用密码:YES)
-
mysql.server 在终端上启动?