gabogabans
gabogabans

Reputation: 3553

Laravel: Edit value only if it appears in the request?

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);

Upvotes: 0

Views: 671

Answers (1)

Peppermintology
Peppermintology

Reputation: 10210

Consider using a Form Request where you preform validation. This will neaten up your controller for a start and also make validation (never trust user input!) reusable.

Assuming validation is successful, calling $request->validated() from inside your controller method will return only the fields present and validated. You can then use either fill($request->validated()) or update($request->validated()).

Upvotes: 2

Related Questions