'1366 Incorrect decimal value Laravel

I have driver model with this condition :

protected $fillable = [
'hourly_rate',
'ot_hourly_rate',
'commission_rate',
'basic_salary',
'join_date',
];

and here's my migration code :

public function up()
{
Schema::table('drivers', function ($table) {
    $table->integer('hourly_rate', 24,2)->nullable()->default(0);
    $table->decimal('ot_hourly_rate', 24,2)->nullable()->default(0);
    $table->decimal('commission_rate', 24,2)->nullable()->default(0);
    $table->decimal('basic_salary', 24,2)->nullable()->default(0);
});
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{   
Schema::table('drivers', function ($table) {
    $table->dropColumn('hourly_rate');
    $table->dropColumn('ot_hourly_rate');
    $table->dropColumn('commission_rate');
    $table->dropColumn('basic_salary');
});
}

the problem is when I try to create driver and didn't fill those field it says error decimal value. decimal can't store empty string as NULL at database ?

note : those field is not required.



Solution 1:[1]

No need to set nullable because you set default value .Try this

 $table->decimal('basic_salary', 24,2)->default(0);

Solution 2:[2]

Try this: Available Column Types

public function up()
{
    Schema::table('drivers', function ($table) {
        $table->integer('hourly_rate')->default(0);
        $table->decimal('ot_hourly_rate', 24,2)->default(0);
        $table->decimal('commission_rate', 24,2)->default(0);
        $table->decimal('basic_salary', 24,2)->default(0);
    });
}

public function down()
{   
    Schema::drop('drivers');
}

Declaration: decimal(p[,s]).

p = Precision - total number of digits stored to both the left and right of the decimal point.

s = Scale the maximum number of digits stored to the right of the decimal point (optional).

Minimum Precision is 1 and Maximum Precision is 38. The Default Precision is 18.

Note: Decimal is equivalent to Numeric.

more details

Solution 3:[3]

I ran into this same issue recently, and the complication was with my input fields.

Submitting this <input name="ex" type="number" step="0.01" placeholder="0.00"> would make the input field equal to "". Regardless of the defaults in your migration you'll still run into an error because the default "" is not a proper decimal.

I chose to simply change my input fields to be more like this, <input name="ex" type="number" step="0.01" value="0" required> That way the default is 0 rather than "", and the required field prevents the submission if the default value is removed and not replaced.

Solution 4:[4]

You could convert the empty values to null by traversing through the inputs.

Ex by doing:

    array_walk_recursive(
        $params,
        function (&$value) {
            if ( $value == "" ) {
                $value = null;
            }
        }
    ); 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nazmul Hasan
Solution 2
Solution 3 Ryan Kozak
Solution 4 Unicco