2017-01-27 5 views
0

私はユーザーとどのようなそれはこのように書きますので、私は私が私の登録で、余分なフィールドを作成されなかったため、別の詳細情報を追加したい:挿入されたユーザー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']), 
     ]); 
    } 

他の詳細を追加するにはどうしたらいいですか?私はまだララベルで新しいです。

答えて

1

あるユーザーは、あなたがこれを参照することができます1つの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 
+0

よかった提案。悪いことも読んでください。 :) – Jerielle

+0

あなたは歓迎ですが、あなたの問題を解決できるかどうか、私の答えが完了したplzマーク –

+0

ok問題はありません:) – Jerielle

2

このような何か試してみてください:

$user = new User();   

$user->name = 'John'; 

$user->save(); 

// Now Getting The Last inserted id 

$insertedId = $user->id; 

echo $insertedId ; 

編集:この行を変更します。

リターンユーザーを::(...)を作成

に$ユーザー=ユーザー: :作成(...)

返信

$user or $user->id; 

ただし、保存が成功するかどうかを確認するチェックを入れてください。

+0

ユーザーデータの作成時に 'return'を削除して、プロファイルを保存する際に移動する必要がありますか? – Jerielle

+0

私の更新された回答を確認してください –

+0

私はそのアプローチを試してみます。 :) – Jerielle

2
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 Creating userInfo database table when user registers

012を使用することができます
1

私はあなたのコードは次のように更新する必要があると思う:

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; 

} 

あなたのため、この作品を願っています!

関連する問題