运行php artisan迁移后未创建数据库表

时间:2018-12-29 21:24:24

标签: php laravel

我使用的是laravel 5.7,我注意到即使对.env进行了正确的更新,并且从我的角度来看一切都很正确,但是运行php artisan migration之后并没有创建数据库表。

经过大量研究,我偶然发现了一段视频,该视频说我必须去AppServiceProver.php并添加如下几行代码:

从以下位置更新AppServiceProver.php:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
       //
    }

    /**
     * Register any application services.

收件人:

<?php

namespace App\Providers; 

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
    }

    /**
     * Register any application services.

在第6行添加了一行代码(使用Illuminate \ Support \ Facades \ Schema;),并在第17行调用了Schema(Schema :: defaultStringLength(191);)。

我立即将这些代码行添加到AppServiceProver.php中,然后运行 php artisan migration ;我所有的表都立即在数据库中创建。

但是对我来说,作为一个学习者来说,不幸的是,那个人说他不知道为什么最初不创建表,以及为什么必须将代码添加到 AppServiceProvider.php 中。他有错误,也偶然发现了答案。

作为一名学习者,我很好奇,想知道为什么我要面对这一挑战,以及为什么这些代码行可以解决这些挑战。

非常感谢您的友好回答。

2 个答案:

答案 0 :(得分:1)

您必须设置defaultStringLength来切换字符集utf8mb4 utf8mb4字符集非常有用,因为如今我们需要支持不仅存储语言字符,而且还存储符号,新引入的表情符号等。希望你明白了!

答案 1 :(得分:0)

抱歉,回复晚了,在运行迁移过程中,您可能会看到

错误
Specified key was too long; max key length is 767 bytes

这是因为

Just For Example

您的迁移可能看起来

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

运行迁移时,它将寻找每个字段的长度

例如

public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name',50);
                $table->string('email',100)->unique();
                $table->timestamp('email_verified_at',150)->nullable();
                $table->string('password',150);
                $table->rememberToken();
                $table->timestamps();
            });
        }

如果您很懒,需要添加逗号长度来添加所有文件

\Schema::defaultStringLength(191);的{​​{1}}方法内使用boot

通过添加此IT,会将文件的长度添加为AppServiceProvider

就这样

相关问题