简体   繁体   中英

How to add id of created user to user_id column

There is a code for adding data to the database

public function store(Comment $commentModel, User $userModel, Request $request)
{
    $userModel->create($request->all());
    $commentModel->create($request->all());
    return redirect()->route('home');
}

Added data to the Comment

protected $fillable = ['user_id', 'text', 'created_at', 'updated_at'];

Added data to User

protected $fillable = ['name', 'created_at', 'updated_at'];

How to pass id of added user to Comment in user_id column

public function store(Comment $commentModel, User $userModel, Request $request)
{
    $user = $userModel->create(['name' => $request->get('name')]);
    $commentModel->create([
        'user_id' => $user->id,
        'text' => $request->get('text)
    ]);
    return redirect()->route('home');
}

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