Я хочу добавить еще детали для пользователей и что я сделал это, я создал дополнительные поля в моей регистрации, так это выглядит следующим образом:Как я могу получить введенный идентификатор пользователя и использовать его в моей таблице профилей в Laravel?
<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>
Тогда в RegisterController я добавил эти поля в проверке:
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'
]);
}
Затем в части проверки у меня нет проблем, но при сохранении в таблице я не знаю, как я могу получить вставленный идентификатор пользователя.
Я создал миграции для профиля, как это:
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('App\User');
}
}
Тогда в моей модели User я добавил отношения также:
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('App\UserProfile');
}
}
Тогда моя проблема заключается в сохранении профиля. Потому что в RegisterController у меня есть только это:
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']),
]);
}
Как добавить дополнительную информацию? Я все еще новичок в Ларавеле.
Хорошо приятное предложение. Я читал это тоже. :) – Jerielle
Добро пожаловать, но если он может решить вашу проблему, PLZ отметить мой ответ завершен –
ОК не проблема :) – Jerielle