我修改了默认的laravel用户表,我是通过创建自己的迁移来实现的。
这是我的迁移示例:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('username', 15)->unique()->after('id');
$table->string('lastname', 15)->unique()->after('username');
$table->string('firstname', 15)->unique()->after('lastname');
$table->string('contact')->nullable()->after('email');
$table->date('birthday')->after('contact');
$table->tinyInteger('status')->default(1)->after('birthday');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('username');
$table->dropColumn('lastname');
$table->dropColumn('firstname');
$table->dropColumn('contact');
$table->dropColumn('birthday');
$table->dropColumn('status');
});
}
我删除了默认laravel表中的列name
,所以它看起来像这样:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
我在AuthController中检查了我的表单中的值,我在获取值时没有问题。这是我在AuthController中添加的内容
protected $username = 'username'; //choose username instead of email
protected $redirectPath = '/dashboard'; //if successful login
protected $loginPath = 'auth/login'; //if not login
protected $redirectAfterLogout = 'auth/login'; //redirect after login
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'username' => 'required|min:8|max:16|unique:users',
'lastname' => 'required',
'firstname' => 'required',
'contact' => 'required',
'birthday' => 'date_format: Y-m-d',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
//dd($data); -- I can get all the values here
// I added my own column in this part to save. Is it correct?
return User::create([
'username' => $data['username'],
'lastname' => $data['lastname'],
'firstname' => $data['firstname'],
'birthday' => $data['birthday'],
'contact' => $data['contact'],
'email' => $data['email'],
'status' => 1,
'password' => bcrypt($data['password']),
]);
}
当我尝试保存数据时,我只能保存这些列
mysql> select * from users \G;
*************************** 1. row ***************************
id: 1
username:
lastname:
firstname:
email: myemail@gmail.com
contact: NULL
birthday: 0000-00-00
status: 1
password: $2y$10$3NkmqZaje4MKzheqPZKbhOIGD7ZlqYRfIP6DJZz4zb4gXVNvFsv2W
remember_token: PCYczF2Y9ts97TvbDOLsiXO5hxkekkxysmMYuIdN5MsaIY8TnroEof6d2jVM
created_at: 2015-09-17 06:56:43
updated_at: 2015-09-17 07:00:35
1 row in set (0.00 sec)
ERROR:
No query specified
如何在数据库中添加新列?
好的,我希望你能帮助我。
答案 0 :(得分:2)
好的,我找到了答案。 我需要将自己的自定义列放在可填写的内容中:
protected $fillable = ['username', 'lastname', 'firstname', 'contact', 'email', 'birthday', 'status', 'password'];
答案 1 :(得分:1)
您不能用这种方式编辑用户表 首先,转到终端并输入以下命令:
MyClass
,然后单击Enter。 之后,转到迁移文件,并在add_new_fields_to_users_table.php中 并编写新表格
php artisan make:migration add_new_fields_to_users_table
然后,转到终端并执行以下操作:
$table->string('username', 15)->unique()->after('id');
$table->string('lastname', 15)->unique()->after('username');
$table->string('firstname', 15)->unique()->after('lastname');
$table->string('contact')->nullable()->after('email');
$table->date('birthday')->after('contact');
$table->tinyInteger('status')->default(1)->after('birthday');
最后进入用户模型,并将新表的名称添加到可填充变量
php artisan migrate
我希望这会对您有所帮助
答案 2 :(得分:0)
伙计们,这是 vk,我想与您分享最有用的信息, 每个注册都不受限制,有些需要额外的元素,如出生数据或有些需要写下你的自我 像那样需要更多的输入元素 在默认注册页面上,它有姓名、电子邮件地址、密码和确认密码 有时我们需要一些其他字段,如电话号码、地址、性别、出生日期等。
对于每个字段相同的程序 为此,我将撰写关于你自己的(描述)元素
您可以手动创建描述字段或添加 第1步: 如何使用 artisan 命令创建一个新列 php artisan make:migration add_phone_field_to_users_table --table=users 如果你想创建任何列 - 必须使用添加列名到表名表 像那样你必须写 再举一个例子 php artisan make:migration add_new_fields_to_users_table --table=users 命令执行后
检查是否在数据库/迁移中创建的文件 文件将如何创建 它将创建具有空数据的函数
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddPhoneFieldToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
}
你必须向这个文件添加一些数据 你想要添加哪个字段
$table->string('contact')->nullable()->after('email');
$table->date('birthday')->after('contact');
$table->tinyInteger('status')->default(1)->after('birthday');
添加如下数据
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddPhoneFieldToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->bigInteger('phone');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('phone');
});
}
}
有关完整文章,请访问此 link