laravel auth寄存器将数据插入两个表

时间:2018-09-13 05:45:21

标签: laravel authentication

我有我的默认身份验证控制器注册。我也想将从用户表创建的emp_id注册到员工表。一旦注册。

我的RegisterController

use App\User;
use App\Employee

public function count_users(){
    $count = User::count();

    return date('y').'-'.sprintf('%04d',$count);
}

protected function create(array $data)
{
    return User::create([
        'emp_id' => $this->count_users(),
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

    return Employee::create([
        'emp_id' => $this->count_users()
    ]);
}

1 个答案:

答案 0 :(得分:1)

请检查代码中的以下行:

return User::create([ .....

上一行创建用户并返回创建的用户。不会调用“ return”下面的任何代码。

请尝试以下代码:

use App\User;
use App\Employee

public function count_users(){
    $count = User::count();

    return date('y').'-'.sprintf('%04d',$count);
}

protected function create(array $data)
{

    $emp_id = $this->count_users();

    $user = User::create([
        'emp_id' => $emp_id,
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

    Employee::create([
        'emp_id' => $emp_id
    ]);

    return $user;
}