Laravel - how to handle not required/optional field in $fillable insert and update

1k Views Asked by At

I have table with below fields.

Only name is required and rest of them is optional

'name','no_of_color','offset_printing_rate','screen_printing_rate','positive_rate','regular_plate_rate','big_plate_rate',

My model

protected $fillable = [
        'name',
        'no_of_color',
        'offset_printing_rate',
        'screen_printing_rate',
        'positive_rate',
        'regular_plate_rate',
        'big_plate_rate',
    ];

but when I don't fill optional field it returns error

SQL: insert into table_name (name, no_of_color, offset_printing_rate, screen_printing_rate, positive_rate, regular_plate_rate, big_plate_rate, updated_at, created_at) values (name, ?, ?, ?, ?, ?, ?, 2021-10-09 14:47:36, 2021-10-09 14:47:36)

1

There are 1 best solutions below

0
On

MySQL always want a value to all cols so in laravel you have to set default for each col or set it nullable but for performance purpose, i recommended you to split optional in other table and connect them to main table by relation this will prevent increasing of null records so you don't have to waste your host space