简体   繁体   中英

How to input user_id in different table (laravel 5.2)

I have 2 tables.

  1. users ( id , name , username , email , password , admin , remember_token )
  2. dosen ( iddosen , user_id , namadosen , birthdate , address )

How to insert user_id and namadosen same as in table users ?

My method :

public function store(CreateDosenRequest $request)
{
    $user = User::create([
        'name' => $request->input('name'),
        'username' => $request->input('username'),
        'email' => $request->input('email'),
        'password' => bcrypt($request->input['password']),
        'admin' => $request->input('admin')
 ]);

      $dosen = Dosen::create([
        'iddosen' => $request->input('iddosen'),
        'name' => $request->input('namadosen'),
        'user_id' => $user->id,
        'address' => $request->input('address'),
        'birthdate' => $request->input('birthdate'),

]);


    return redirect('admin')->with('message', 'Success!');              
}

following code doesn't work

 'user_id' => $user->id,

Just answering this for consistency sake.

As stated in the comments, the issue was that user_id wasn't specified as a $fillable field inside the Dosen model.

Adding user_id to the $fillable array solved the issue for OP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM