我如何在表单提交时的employee_id下将雇员表ID(自动递增)提交给用户表

问题描述 投票:0回答:1
public function store(Request $request)
{

     Employees::create([

        'first_name'=>$request['first_name'],
        'last_name'=>$request['last_name'],
        'email'=>$request['email'],
        'contact_no'=>$request['contact_no'],
        'join_date'=>$request['join_date'],
        'date'=>$request['date'],
        'employee_no'=>$request['employee_no'],
        'no'=>$request['no'],
        'profile'=>$request['profile'],
        'dob'=>$request['dob'],
        'leave_eligible_date'=>$request['leave_eligible_date'],
        'leave_eligible_date'=>$request['leave_eligible_date'],
        'employee_type_id'=>$request['employee_type_id'],
        'employee_designation_id'=>$request['employee_designation_id'],
        'employee_department_id'=>$request['employee_department_id'],
        'organization_hierarchy'=>$request['organization_hierarchy'],
        'direct_contact_person_id'=>$request['direct_contact_person_id'],
        'status'=>$request['status']
    ]);

我想将Employee表ID发送到表单Submit的employee_id列下的用户表中

      User::create([
        'name'=>$request['first_name'],
        'email'=>$request['email'],
        'photo'=>$request['profile'],   
        'employee_id'=>$request['????'], // i want send this line
        'password'=>Hash::make($request['password'])
     ]);
}

上面的代码会将数据插入到两个表中,但是我希望将自动递增的员工表ID插入到表单提交的employee_id列下的用户表中。

laravel-5 laravel-5.6
1个回答
0
投票

只需获取Employee Create函数的响应并获取Employee ID,然后将其传递给User create函数。

$employee = Employees::create([

    'first_name'=>$request['first_name'],
    'last_name'=>$request['last_name'],
    'email'=>$request['email'],
    'contact_no'=>$request['contact_no'],
    'join_date'=>$request['join_date'],
    'date'=>$request['date'],
    'employee_no'=>$request['employee_no'],
    'no'=>$request['no'],
    'profile'=>$request['profile'],
    'dob'=>$request['dob'],
    'leave_eligible_date'=>$request['leave_eligible_date'],
    'leave_eligible_date'=>$request['leave_eligible_date'],
    'employee_type_id'=>$request['employee_type_id'],
    'employee_designation_id'=>$request['employee_designation_id'],
    'employee_department_id'=>$request['employee_department_id'],
    'organization_hierarchy'=>$request['organization_hierarchy'],
    'direct_contact_person_id'=>$request['direct_contact_person_id'],
    'status'=>$request['status']
]);

$user = User::create([
    'name'=>$request['first_name'],
    'email'=>$request['email'],
    'photo'=>$request['profile'],   
    'employee_id'=> $employee->[id_column], // here use the column of employee id
    'password'=>Hash::make($request['password'])
 ]);
© www.soinside.com 2019 - 2024. All rights reserved.