【问题标题】:Creating users table in Laravel在 Laravel 中创建用户表
【发布时间】:2019-04-14 21:16:57
【问题描述】:

我对 laravel 的 users 表有些问题。我很久以前已经删除了那些默认表。现在我正在尝试使用 Auth 但我无法注册。因为数据库中没有表。但我也无法使用php artisan migrate. 创建表,因为我已经删除了那些迁移表。所以我想再次创建这些表。但我找不到默认文件。

并且 make:auth 并没有带来表格......我需要自己重新创建它。我记得有两个不同的表然后一个是用户和重置密码?有谁知道我在哪里可以再得到这些桌子?

【问题讨论】:

  • 我想你可以使用公共 git 中的default ones 来替换你已经删除的那些。虽然它可能无法完全解决您的问题,但它是一个很好的起点。
  • 是否需要创建默认用户表?

标签: php laravel authentication


【解决方案1】:

您可以从 laravel 存储库中检索那些已删除的迁移: https://github.com/laravel/laravel/tree/master/database/migrations

2014_10_12_000000_create_users_table.php:

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    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();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

2014_10_12_100000_create_password_resets_table.php:

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('password_resets');
    }
}

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • 请对该代码添加一些解释,以便其他人可以从中学习
  • @NicoHaase 我认为这对于了解 laravel 的人来说很清楚,但如果可以的话,我肯定会添加一些解释。
【解决方案2】:

只需运行这些命令

php artisan make:migration create_users_table
php artisan make:migration create_password_resets_table

在您的迁移中创建用户表

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('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

之后运行

php artisan migrate:refresh

PS:这将重置您的数据库 或者直接运行

php artisan migrate

编辑:如果遇到错误1071 Specified key was too long; max key length is 767 bytes

在你的AppServiceProvider.php 添加这个

use Illuminate\Support\Facades\Schema; //this

public function boot()
{
    Schema::defaultStringLength(191); //this
}

【讨论】:

  • 谢谢你,这就是我想要的!我检查你的答案!
  • 但它给了我这个错误:SQLSTATE[42000]:语法错误或访问冲突:1071 指定的密钥太长;当我尝试迁移时,最大密钥长度为 767 字节(SQL:alter table users add unique users_email_unique(email))
  • 使用 Illuminate\Support\Facades\Schema 编辑您的 AppServiceProvider.php;公共函数 boot() { Schema::defaultStringLength(191); }
  • 已编辑答案请检查
  • 文件位于 {ProjectLocation}/app/Providers/AppServiceProvider.php
【解决方案3】:

你应该运行下面的命令:

php artisan make:auth

然后运行下面的命令

php artisan migrate

【讨论】:

  • 你读过这个问题吗? make auth 不带用户和重置密码表
  • @Snickers:请检查迁移文件夹是否有任何用户迁移文件
  • 不,没有。因为我很久以前删除了那些。并且 make:auth 不会带来这些。
  • @Snickers 您需要安装新版本的 laravel 并将用户迁移文件从新版本复制到当前版本,然后您可以执行上述命令,它会起作用...
猜你喜欢
  • 1970-01-01
  • 2014-11-16
  • 2020-10-10
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2016-11-28
  • 2017-06-29
  • 2015-11-25
相关资源
最近更新 更多