Home » Php » php – error is occured at the run time when 2 tables are insering in 1 function at the same time

php – error is occured at the run time when 2 tables are insering in 1 function at the same time

Posted by: admin February 25, 2020 Leave a comment

Questions:

//Controller

 public function create(Request $request)
    {
        // dd($request->all());
        $this->validate($request, [
             'emp_nm' => 'required',
             'emp_email' => 'required | email',
             'emp_password' => 'required' ,
             'mobile' => 'required | max:20' ,
             'emp_type_id' => 'required',
             'emp_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
         ]);
        $employee = new Employee();
        $employee->emp_nm = $request->input('emp_nm');
        $employee->emp_email = $request->input('emp_email');
        $employee->emp_password = Hash::make($request->input('emp_password'));
        $employee->mobile = $request->input('mobile');
        $employee->emp_type_id = $request->input('emp_type_id');
        if ($request->hasfile('emp_img')) {

            $file = $request->file('emp_img');
            $extention = $file->getClientOriginalExtension(); // get img extension
            $filename = time() . '.' . $extention;
            $file->move('upload/employee/',$filename);
            $employee->emp_img = $filename;

        }else{
            return $request;
            $employee->emp_img = '';
        }
        $employee->save();

         $id = $employee->id;
         $created_at = $employee->created_at;
         $updated_at = $employee->updated_at;
         $post =  Employee::find($id);
         //  $created = Employee::get('created_at');
         // $updated = Employee::get('updated_at');
        $employee = DB::table('employees')->get();

        $admin = new Admin();
        $admin->name = $request->input('emp_email');
        $admin->password =  $request->input('emp_password');
        $admin->employee_id =  $post;  
        // $admin->created_at =  $created;      
        // $admin->updated_at =  $updated;  
        $admin->save();
        return redirect()->back();


    }

//ERROR

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer
value:
‘{“id”:1,”emp_nm”:”admin”,”emp_email”:”[email protected]”,”emp_password”:”$2y$10$NLkdEn1ohHvHKU/uummrvOrwhJ3iEy7PPfxpk9dj9u6.pOVu1’ for column mycrm.admins.employee_id at row 1 (SQL: insert into
admins (name, password, employee_id, updated_at,
created_at) values ([email protected], [email protected]#,
{“id”:1,”emp_nm”:”admin”,”emp_email”:”[email protected]”,”emp_password”:”$2y$10$NLkdEn1ohHvHKU/uummrvOrwhJ3iEy7PPfxpk9dj9u6.pOVu10YTa”,”mobile”:9426399403,”emp_type_id”:”1″,”emp_img”:”1581590201.jpg”,”created_at”:”2020-02-13
10:36:41″,”updated_at”:”2020-02-13 10:36:41″}, 2020-02-13 10:36:41,
2020-02-13 10:36:41))

In the Controller the code is inserting 2 tables at the same time now it is return the error of date formate if i am assign date so it is..
Unexpected data found.
Trailing data
so how i can solve this error??

How to&Answers:

Your code is wrong $admin->employee_id = $post;
Try to use $admin->employee_id = $post->id;

Answer:

Your first table insertion is correct. But when you insert record in the admin is wrong:

So update your code:

    $id = $employee->id;
    $admin = new Admin();
    $admin->name = $request->input('emp_email');
    $admin->password =  $request->input('emp_password');
    $admin->employee_id =  $id;  
    $admin->created_at =  $created;      
    $admin->updated_at =  $updated;  
    $admin->save();