简体   繁体   English

如何将创建用户的 id 添加到 user_id 列

[英]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如何将添加用户的 id 传递给 user_id 列中的评论

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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