Laravel 5 Form request and user register -


the problem is, need simultaneously register user , enter table data forms , assign id

i tried

$data = request::all();      $user = new app\user();     $user->username = $data['name'];     $user->email = $data['email'];     $user->password = bcrypt($data['password']);     $user->save(); 

how new user id?

that record other data such as

$question = new app\question(); $question->name_q = $data['name_q']; $question->body_q = $data['body_q']; $question->user_id = ? 

you should have relationship defined can insert data without creating objects of 2 different tables. lets first @ case , show how better way.

$data = request::all();  $user = new app\user(); $user->username = $data['name']; $user->email = $data['email']; $user->password = bcrypt($data['password']); $user->save();  $question = new app\question(); $question->name_q = $data['name_q']; $question->body_q = $data['body_q']; $question->user_id = $user->id; $question->save(); 

and done. in case have create objects of 2 different tables , write ok of happens when have insert data many many tables @ go. lets see that:

define relationship first have, lets see should have in model :

//user model should have questions function public function questions()   {       return $this->hasmany('app\question');   }  //question model should have user model public function user()  {         return $this->belongsto('app\user');  } 

and

$user = new app\user(); $user->username = $data['name']; $user->email = $data['email']; $user->password = bcrypt($data['password']); $user->save();   $user->questions()->create([             'field' => $somevariable,              'field' => $someothervariable         ]); //here questions function name in user model 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -