在Laravel中创建用户表

时间:2018-11-12 07:04:35

标签: php laravel authentication

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

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

3 个答案:

答案 0 :(得分:6)

只需运行这些命令

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

在您的迁移中create_users_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();
        });
    }

在您的迁移中,create_password_resets_table

 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
}

答案 1 :(得分:2)

您可以从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');
    }
}

答案 2 :(得分:0)

您应运行以下命令:

php artisan make:auth

然后在命令下运行

php artisan migrate