Home > Mobile >  1366 Incorrect integer value error while storing data into the database in laravel
1366 Incorrect integer value error while storing data into the database in laravel

Time:04-05

In my laravel application I have two user roles, admins and general users.

An admin can add data in a general user profiles as well.

In my db table, I have two columns to save user id and the added_by user's id.

If a general user add record to their own profile, both this column should include general user's user id.

But if an admin user did this, then added_by has to be filled with admin's user id.

I'm fetching the general user's user id from the URL.

My function looks like this at the moment (Here, I've included only the related part for this question)

$employee_Id=$request->get('emp_id');
if(is_numeric($employee_Id)){
            $employee_Id=$request->get('emp_id');
            $user = User::where('id',$employee_Id);
            $addedBy_User = User::find(auth()->user()->id);
//If the employer tried to add their stats    
}else{
            $user=User::find(auth()->user()->id);
            $employee_Id=$user->id;
            $addedBy_User =User::find(auth()->user()->id);
}
$statsUser = StatsUser::create([
            'stat_id'               => $stat->id,
            'user_id'               => $employee_Id,
            'added_by'              => $addedBy_User,
            
        ]);

But this gives me an error saying, 1366 Incorrect integer value what I'm doing wrong and where should I fix?

The complete error message as follows,

1366 Incorrect integer value: '{\"id\":869,\"language\":\"en\",\"first_name\":\"Test\",\"last_name\":\"Name2\",\"email\":\"[email protected]\",\"email_verified_at\":\"2022-03-...' for column `cricketstats`.`stats_user`.`added_by` at row 1 

CodePudding user response:

It makes sense since in your if statement you are checking if $employee_Id is numeric. then look what you have in the else statement:

//...
} else {
    $user=User::find(auth()->user()->id);
    $employee_Id=$user->id;
    $addedBy_User = $employee_Id; // <--
}

So, when $employee_Id is not numeric then $addedBy_User is also not numeric -> not an integer. Hence, the exception is thrown.

CodePudding user response:

$addedBy_User = User::find(auth()->user()->id);

StatsUser::create([ 'stat_id' => $stat->id, 'user_id' => $employee_Id, 'added_by' => $addedBy_User,

]);

$addedBy_User is an object , Solution -> $addedBy_User->id

  • Related