Laravel 5.2 - 如何获取新创建用户的 id 并将其传递给控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37176669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:46:53  来源:igfitidea点击:

Laravel 5.2 - How to get id of newly created user and pass it to a controller

laravel

提问by noname

I've never used Laravel before and I'm working on a 3 part registration using laravels default user registration & auth for step 1 of the form. Each step has its own table. The id column of step 1s table is used to link all 3 tables.

我以前从未使用过 Laravel,我正在使用 laravel 默认用户注册和表单第 1 步的身份验证进行 3 部分注册。每个步骤都有自己的表。step 1s 表的 id 列用于链接所有 3 个表。

Currently im using $id = Auth::user()->idto get the logged in users id in the step 2 controller. Instead how can I route/pass the id after the user is created to step 2 controllers store method and redirect the view to step 2?

目前我正在使用$id = Auth::user()->id第 2 步控制器中的登录用户 ID。相反,如何在将用户创建到第 2 步控制器存储方法并将视图重定向到第 2 步之后路由/传递 id?

in authcontroller

在 authcontroller 中

protected function create(array $data)
{

    return User::create([
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'firstName' => $data['firstName'],
        'middleName' => $data['middleName'],
        'lastName' => $data['lastName'],
    ]);
}

in Illuminate\Foundation\Auth\RegistersUsers;

在 Illuminate\Foundation\Auth\RegistersUsers 中;

public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    Auth::guard($this->getGuard())->login($this->create($request->all()));

    // pass id to store method in profile and redirect to profileinfo 
    return view('user/profileinfo');
}

}

采纳答案by Alexey Mezenin

Instead of using create()method which returns trueor false, you should use save()method which returns the model:

您应该使用返回模型的create()方法,而不是使用返回trueor 的方法:falsesave()

$id = $data->save()->id;

But if you still need short syntax, you can use mass assignment:

但是如果你仍然需要简短的语法,你可以使用mass assignment

$userModel = new User($data);
$userModel->save();
$id = $userModel->id;

回答by allaghi

You can change create method in AuthController in this way :

您可以通过以下方式更改 AuthController 中的 create 方法:

protected function create(array $data)
{
    $user =  User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),

    ]);

    $id= $user->id; // Get current user id

    return $user;
}

回答by Matt Komarnicki

Try this approach:

试试这个方法:

$data = new FooModel(); // or in your case User();
$data->column_1 = 'value_1';
$data->column_2 = 'value_2';
$data->save();

and now after calling save(), $data->idshould give you last inserted ID, so that you can use it wherever you want.

现在在调用之后save()$data->id应该给你最后插入的 ID,这样你就可以在任何你想要的地方使用它。

Just keep in mind, that if the PK column (the one with ID) is not auto-increment, it will always return 0.

请记住,如果 PK 列(带有 ID 的列)不是 auto-increment,它将始终返回 0。

回答by Shalu Singhal

You can change your create()function to

您可以将您的create()功能更改为

protected function create(array $data)
{
  $user = new User();
  $user->email = $data['email'];
  $user->password = bcrypt($data['password']),
  $user->firstName = $data['firstName'],
  $user->middleName = $data['middleName'],
  $user->lastName = $data['lastName'];
  $user->save();

  return $user->id;
 }