简体   繁体   English

我应该如何保持一致性?Laravel。 我想在生成新用户时插入有关用户的数据

[英]How should I keep consistency?Laravel. I would like to insert data about user when a new user is generated

How should I keep consistency?我应该如何保持一致性? I would like to do "insert" to another table when a new user is registered.当新用户注册时,我想“插入”到另一个表。

tables user //data equals request that validated.表用户//数据等于验证的请求。

            'name' => $data['name'],
            'email' => $data['email'],
            'profile' => $data['profile'],
            'adress' => $data['adress']

additional_table worker_id has a foreign key of the user_id. additional_table worker_id 具有 user_id 的外键。

'worker_id' =>  $user->id,  
'place' => $request->place

Laravel Auth register. Laravel 授权注册。

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

        event(new Registered($user = $this->create($request->all())));

        if($request->user_type == 1)
        {
            DB::table('additional')->updateOrInsert(
                ['worker_id' =>  $user->id,  
                ['place' => $request->place)]
            );
        }
        $this->guard()->login($user);

        if ($response = $this->registered($request, $user)) {
            return $response;
        }

        return $request->wantsJson()
                    ? new Response('', 201)
                    : redirect($this->redirectPath());
    }

Here's something that might work:这是可能有用的东西:

DB::table('additional')->insertOrIgnore([
    ['worker_id' => $user->id, 'place' => $request->place]
]);

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

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