在 Laravel 中创建用户表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53257296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating users table in Laravel
提问by Snickers
I have some trouble with the laravel's users table. I already deleted those default table long time ago. And now I am trying to use Auth but I can't register. because there is no table in the database. But also i can't create table with php artisan migrate.
because I already deleted those migration tables. So I want create those tables once more. But I couldn't find the default files.
我在 Laravel 的用户表上遇到了一些麻烦。很久以前我已经删除了那些默认表。现在我正在尝试使用 Auth 但我无法注册。因为数据库中没有表。但我也无法创建表,php artisan migrate.
因为我已经删除了这些迁移表。所以我想再次创建这些表。但是我找不到默认文件。
And make:auth is doesn't bring the table... I need to recreate it by myself. I remember there two diffrent tables back then one is users and reset password? Do anyone know where can I get thoese tables again?
而 make:auth is 不带表......我需要自己重新创建它。我记得有两个不同的表,然后一个是用户和重置密码?有谁知道我在哪里可以再次获得这些桌子?
回答by Suraj Tiwari
Just run these commands
只需运行这些命令
php artisan make:migration create_users_table
php artisan make:migration create_password_resets_table
In your migration create_users_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();
});
}
In your migration create_password_resets_table
在您的迁移中 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();
});
}
after that run
在那次跑步之后
php artisan migrate:refresh
PS: This will reset your database Or just run
PS:这将重置您的数据库或只是运行
php artisan migrate
EDIT: If facing error 1071 Specified key was too long; max key length is 767 bytes
编辑:如果遇到错误 1071 Specified key was too long; max key length is 767 bytes
In your AppServiceProvider.php
add this
在你AppServiceProvider.php
添加这个
use Illuminate\Support\Facades\Schema; //this
public function boot()
{
Schema::defaultStringLength(191); //this
}
回答by AmirhosseinDZ
You can retrieve those deleted migrations from laravel repository : https://github.com/laravel/laravel/tree/master/database/migrations
您可以从 laravel 存储库中检索那些已删除的迁移:https: //github.com/laravel/laravel/tree/master/database/migrations
2014_10_12_000000_create_users_table.php :
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 :
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');
}
}
回答by AmirhosseinDZ
You should run below command:
您应该运行以下命令:
php artisan make:auth
then run below command
然后运行下面的命令
php artisan migrate