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

Gabotronix's avatar

Edit value only if it appears in the request?

Hi everybody, in my app the user can update the info of stripe connected account, however I ONLY want to actullay update the value of the fields that appear in the request payload, I could do this with a simple if check but the way I update the stripe array method makes this issue more complicated .

Is there any syntax sugar or trick to make this easier.

How my update method looks;

public function editConnectedAccount(Request $request)
    {     
        $account = Account::retrieve($request->connectedAccountId);

        Account::update(
            $request->connectedAccountId,
            [
                'type' => 'custom',
                'country' => 'ES',
                'email' => $request->userEmail,
                'business_type' => 'individual',
                'tos_acceptance' => [ 'date' => Carbon::now()->timestamp, 'ip' => '83.46.154.71' ],
                'individual' => 
                [ 
                    'dob' => [ 'day' => $request->userDOBday, 'month' => $request->userDOBmonth, 'year' => $request->userDOByear ], 
                    'first_name' => $request->userName, 
                    'email' => $request->userEmail, 
                    'phone' => $request->userPhone,
                    'last_name' => $request->userSurname,
                    //'ssn_last_4' => 7871,  
                    'address' => [ 'city' => $request->userBusinessCity, 'line1' => $request->userBusinessAddress, 'postal_code' => $request->userBusinessZipCode, 'state' => $request->userBusinessCity ]
                ],
                'business_profile' => 
                [ 
                    'mcc' => 5812, //got it
                    'description' => '',
                    //'url' => 'https://www.youtube.com/?hl=es&gl=ES', //got it
                ],
                'capabilities' => [
                  'card_payments' => ['requested' => true],
                  'transfers' => ['requested' => true],
                ],
              ]
        );
        

        return response()->json([
            'account' => $account,
        ], 200);
0 likes
2 replies
Jaytee's avatar

As far as I always knew, the attributes wouldn't be updated if the value was the same, so passing the value won't matter.

Do you have access to methods on the request: whenHas or whenFilled, which accepts a closure, to output an array of values you wish to add.

$request->whenFilled('some.key', function () {
            return [
                'some' => 'value'
            ];
        });

Please or to participate in this conversation.