我想为用户添加另一个详细信息,我所做的是我在注册中创建了一个额外的字段,因此如下所示:
<h1>Other Details</h1>
<div class="form-group{{ $errors->has('phone') ? ' has-error' : '' }}">
<input id="phone" type="text" class="form-control" name="phone" placeholder="phone number" />
@if ($errors->has('phone'))
<span class="help-block">
<strong>{{ $errors->first('phone') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('address') ? ' has-error' : '' }}">
<textarea name="address" id="address" placeholder="your address" class="form-control"></textarea>
@if ($errors->has('address'))
<span class="help-block">
<strong>{{ $errors->first('address') }}</strong>
</span>
@endif
</div>
然后在注册控制器中,我在验证中添加了这些字段:
protected function validator(array $data)
{
return Validator::make($data, [
'lastname' => 'required|max:255',
'firstname' => 'required|max:255',
'username' => 'required|max:16|unique:users',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'phone' => 'required',
'address' => 'required'
]);
}
然后在验证部分我没有问题,但在保存到表中时我不知道如何获取插入的用户 ID。
我为配置文件创建了一个迁移,如下所示:
public function up()
{
Schema::create('user_profile', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('phone');
$table->text('address');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
然后我还为用户配置文件创建了一个模型
class UserProfile extends Model
{
protected $table = 'user_profile';
public function user() {
return $this->belongsTo('AppUser');
}
}
然后在我的用户模型中,我还添加了关系:
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'username',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function profile() {
return $this->hasOne('AppUserProfile');
}
}
然后我的问题在于保存配置文件。因为在寄存器控制器中我只有这个:
protected function create(array $data)
{
return User::create([
'name' => title_case($data['lastname']) . ' ' . title_case($data['firstname']),
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
如何添加我的其他详细信息?我还是拉拉维尔的新人。
尝试这样的事情:
$user = new User();
$user->name = 'John';
$user->save();
// Now Getting The Last inserted id
$insertedId = $user->id;
echo $insertedId ;
编辑:更改此行:
返回用户::创建(...)
自
$user = 用户::创建(...)
并返回
$user or $user->id;
但是检查以确认保存是否成功。
一个用户有一个user_profile您可以参考以下内容: https://laravel.com/docs/5.3/eloquent-relationships#inserting-and-updating-related-models
请尝试此方法:
$user = new User();
$user->name = 'John Doe';
// rest of the user details
$user->save();
// You can get INSERTED USER_ID
$inserted_user_id = $user->id
// Or you can update user_profile
// without your INSERTED USER_ID very simple
$profile = new Profile();
$profile->address1 = 'Some address';
// rest of address details
$user->profile()->save($profile);
// this is working fine
您的代码需要更新,如下所示:
protected function create(array $data)
{
$userId = DB::table('users')->insertGetId(array(
'name' => title_case($data['lastname']) . ' ' . title_case($data['firstname']),
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
));
echo $userId;
}
希望这对你有用!
protected function create(array $data)
{
$user = User::create([
'name' => title_case($data['lastname']) . ' ' . title_case($data['firstname']),
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Profile::create([
'user_id' => $user->id,
'phone' => '',
'address' => '',
]);
return $user
}
或者您可以使用$user->userInfo()
$user->userInfo()->save(Profile::create([
'user_id' => $user->id,
'phone' => '',
'address' => '',
//whatever information you need to save to the userInfo table - if any
]));
Laravel在用户注册时创建用户信息数据库表