私はユーザーとどのようなそれはこのように書きますので、私は私が私の登録で、余分なフィールドを作成されなかったため、別の詳細情報を追加したい:挿入されたユーザーIDを取得し、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'
]);
}
私は問題はありませんが、テーブルに保存すると、私はどのように私は挿入されたユーザー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');
});
}
は、その後、私はまた私のUserモデルに続いて、ユーザープロファイル
class UserProfile extends Model
{
protected $table = 'user_profile';
public function user() {
return $this->belongsTo('App\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マーク –
ok問題はありません:) – Jerielle