Php 当新用户向应用程序注册时添加表单字段

Php 当新用户向应用程序注册时添加表单字段,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我试图在Laravel 5创建的users表中添加一个字段。我修改了迁移以添加该字段: class CreateUsersTable extends Migration { public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->

我试图在Laravel 5创建的
users
表中添加一个字段。我修改了迁移以添加该字段:

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->string('my_new_field'); // Added this field
            $table->rememberToken();
            $table->timestamps();
        });
    }
    ...    
}
我使用的是Laravel提供的默认身份验证系统。每当新用户注册时,我需要将
my_new_字段
设置为某个值。我如何(在哪里)做到这一点


AuthController
处理身份验证过程。它使用
trait AuthenticatesAndRegistersUsers
,其中
postRegister()函数处理注册请求。但是实际值插入到哪里呢?

只需在字段中添加一个默认值,如下所示:

class CreateUsersTable extends Migration {

    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->string('my_new_field')->default('init_value'); // Added this field
            $table->rememberToken();
            $table->timestamps();
        });
    }
    ...    
}
或者,如果已执行创建迁移,只需使用新迁移添加列:

class AddMyNewFieldToUsersTable extends Migration {
    public function up()
    {
        Schema::table('users', function($table) {
            $table->string('my_new_field')->default('init_value'); // Added 
        });
    }

    public function down()
    {
        Schema::table('users', function($table) {
            $table->dropColumn('my_new_field');
        });
    }
}
如果不希望使用db默认值,也可以在控制器中的
存储
方法中设置此值:

public class UsersController extends BaseController {

    // ...

    public function store(Request $request) {
        $user = new User;
        $user->fill($request->all());
        $user->my_new_field = 'init_value';
        $user->save();

        // return whatever
    }
}
编辑 根据您在评论中提供的信息,这里有一些指导:

AuthController
(/app/Http/Controllers/Auth/AuthController.php)中添加以下方法(这将覆盖可找到的AuthenticatesAndRegistersUsers特征的默认值)

我不太确定这是不是开箱即用,但它应该会让你重新开始

App\Services\register
中的
create()
方法负责创建一个新的
User
实例。我将字段添加到此函数中:

   /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    public function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'my_new_field' => 'Init Value'
        ]);
    }
据报道,

修改新用户注册时所需的表单字段 使用您的应用程序,您可以修改
App\Services\register
班级。此类负责验证和创建新用户 你的申请

注册器的
验证程序
方法包含验证规则
对于应用程序的新用户,而
注册员负责在您的系统中创建新的用户记录
数据库您可以随意修改这些方法中的每一种。
AuthController
通过方法调用注册器 包含在
认证和注册用户
特征中

2016年2月3日更新 拉威尔5.2 app/Services/registrator.php文件中的设置已移动到app/Http/Controllers/Auth/AuthController.php

我还必须在
用户
模型中填写此字段:

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
     ...
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password', 'my_new_field'];
    ...
}
打开位于的特征(对于Laravel 5.2 AuthController)

在这里,您可以轻松找到负责登录和注册用户的有趣方法

这些是

getRegister() // responsible for showing registration form

postRegister() // responsible for processing registration request

getLogin() // responsible for showing login form

postLogin() // responsible for authentication user
每次访问特定路径(auth/register或auth/login),即用户注册和用户登录时,都会调用这些方法


希望这能澄清一些概念

是的,当为所有用户设置默认值时,这将起作用。如果不是所有用户的值都相同怎么办?那么您需要提供有关条件的更多信息;)那是哪个控制器?你存储用户的控制器。。。可能是
userscoontroller
?是的,可以。但我找到了一个。谢谢你的更新…这让我发疯了!@!
/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesAndRegistersUsers.php
getRegister() // responsible for showing registration form

postRegister() // responsible for processing registration request

getLogin() // responsible for showing login form

postLogin() // responsible for authentication user