Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Marcolino922's avatar

Save only inputs compiled from an array

Hi, let me explain, I have an array where I passed some data to save in the database, there are four, but two of these can be null.

Unfortunately even if they are null, the rows in the database are still created with NULL values. I would like them to be saved only when the inputs are compiled.

This is my code, I wanted to try something with the "filled" or "has" method, but I can't use curly brackets inside arrays and I don't know what solution to adopt.

$custom_fields = [
                ['name'=>$request->name_1, 'value'=>$request->value_1],
                ['name'=>$request->name_2, 'value'=>$request->value_2],
                ['name'=>$request->name_3, 'value'=>$request->value_3],
                ['name'=>$request->name_4, 'value'=>$request->value_4],
            ];

            Detail::insert($custom_fields);
0 likes
0 replies

Please or to participate in this conversation.