我正在尝试向 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_field
设置为某个值。我该如何(以及在哪里)做到这一点?
AuthController
处理身份验证过程。它使用 postRegister()
函数处理注册请求的trait AuthenticatesAndRegistersUsers
。但是实际值插入在哪里?
AppServicesRegistrar
中的 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'
]);
}
根据文件,
修改新用户注册时所需的表单域 使用您的应用程序,您可以修改
AppServicesRegistrar
.class。此类负责验证和创建新用户 您的应用程序。注册器的
validator
方法包含验证规则 对于应用程序的新用户,create
而 注册商负责在您的 数据库。您可以根据需要自由修改这些方法中的每一种。 注册器由AuthController
通过方法调用 包含在AuthenticatesAndRegistersUsers
性状中。
更新 Feb 03, 2016拉拉维尔 5.2在 app/Services/Registrar.php 文件中找到的设置已移至 app/Http/Controllers/Auth/AuthController.php
我还必须使此字段可在User
模型中填充:
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
...
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password', 'my_new_field'];
...
}
字段添加一个默认值,如下所示:
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 默认值,也可以在 store
方法的控制器中设置此值:
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-trait的默认值,可在此处找到)
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails())
{
$this->throwValidationException(
$request, $validator
);
}
$user = $this->create($request->all()); // here the values are inserted
$user->my_new_field = 'init_value'; // here would be the place to add your custom default
$user->save(); // maybe try to do all that in a single transaction...
Auth::login($user);
return redirect($this->redirectPath());
}
我不太确定这是否开箱即用,但它应该让你入门。
打开位于(对于 Laravel 5.2 AuthController)的特征
/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
每次访问特定路由(auth/register或auth/login)时都会调用这些方法,即用户注册和用户登录
希望这会清除一些概念!