- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试向 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
处理身份验证过程。它使用 trait AuthenticatesAndRegistersUsers
,其中 postRegister()
函数处理注册请求。但是实际值在哪里插入?
最佳答案
App\Services\Registrar
中的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'
]);
}
根据documentation ,
To modify the form fields that are required when a new user registers with your application, you may modify the
App\Services\Registrar
class. This class is responsible for validating and creating new users of your application.The
validator
method of the Registrar contains the validation rules for new users of the application, while thecreate
method of the Registrar is responsible for creating new User records in your database. You are free to modify each of these methods as you wish. The Registrar is called by theAuthController
via the methods contained in theAuthenticatesAndRegistersUsers
trait.
2016 年 2 月 3 日更新拉维尔 5.2app/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'];
...
}
关于php - 新用户注册应用程序时添加表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28957306/
我尝试通过正则表达式将文本中的单引号更改为双引号。 (单字)示例:我走了。 You gona fly to planet 'Ziqtos' => 我需要在 I'm 中保留单引号,并在 You gona
我正在构建一个 API,其中大部分将包含 JSON 和 HTML 内容。但是一些非常具体的端点只呈现 true 或 false,并且还在 POST 中接受 true 或 false。这是请求或响应的整
我是一名优秀的程序员,十分优秀!