Rade Iliev
Rade Iliev

Reputation: 229

Laravel truncated data

I have small problem with Laravel truncated data. I'm passing to my backend value that can be 'percent' or 'amount' as defined in table, but it can be nulled in table as well, but every time when I try to set it to null I have error like:

{ "discount_currency": [ "The value you have entered is invalid." ] }

or when I delete enum from handling request it says

[ { "title": "Something went wrong!", "message": "SQLSTATE[01000]: Warning: 1265 Data truncated for column 'discount_currency' at row 1 (SQL: update `products` set `discount_amount` = 0, `discount_currency` = null, `products`.`updated_at` = 2021-12-14 13:50:28 where `id` = 3)" } ]

So I'm passing data threw API from my Vue, and request for this pram looks like:

discount_currency' => [
                'string',
                'nullable',
                new EnumValue(EnumProductDiscountCurrencies::class, false) ?? null
            ]

So if I delete new EnumValue... I have secound error. Enum is defined simply:

public const percent = 'percent';
public const euro = 'euro';

What am I doing wrong here? This table can be nulled, and I can change it directly from database, why I can't handle it here? btw there is default value defined in database. This is the migrations

Schema::table('products', function (Blueprint $table) {
            $table->enum('discount_currency', ['percent', 'euro'])->default('percent');
        });

Upvotes: 1

Views: 545

Answers (1)

Giles Bennett
Giles Bennett

Reputation: 1636

Your migration doesn't allow for the discount_currency column to be nullable. You would need to add :

->nullable();

to the end of it to allow null values.

Upvotes: 1

Related Questions