如何使用eloquent在Laravel中存储具有用户ID的输入数据?

时间:2018-06-13 07:53:44

标签: laravel eloquent laravel-5.5

我在数据库中有四个字段: Profile_id(主键),user_id,First_name,Last_name

 public function store(Request $request)
 {
    $this->validate($request, [
        'first_name' => 'required|max:50',
        'last_name' => 'required|max:50',
    ]);
    $user_id= Auth::user()->id;
    $postdata = $request->all();
    profile::create($postdata);
    return redirect('profile');
}

这只存储First_name和last_name,如何与其他人保存用户ID。

1 个答案:

答案 0 :(得分:3)

public function store(Request $request)
{
$this->validate($request, [
    'first_name' => 'required|max:50',
    'last_name' => 'required|max:50',
]);
$data['user_id'] = Auth::user()->id;
$data['first_name'] = $request['first_name'];
$data['last_name'] = $request['last_name'];
profile::create($data);
return redirect('profile');
}

尝试这种方式存储数据