John Davidson

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

0 comments
Message:


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


My Answer:
You can use the `array_filter` function in PHP to filter out the keys that have null values in the request payload. This way, you can update only the fields that are present in the request.

Here's an example of how you can modify your code to achieve this:

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

$updateData = array_filter([
'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,
'address' => ['city' => $request->userBusinessCity, 'line1' => $request->userBusinessAddress, 'postal_code' => $request->userBusinessZipCode, 'state' => $request->userBusinessCity]
],
'business_profile' => [
'mcc' => 5812,
'description' => '',
],
'capabilities' => [
'card_payments' => ['requested' => true],
'transfers' => ['requested' => true],
],
]);

Account::update(
$request->connectedAccountId,
$updateData
);

return response()->json([
'account' => $account,
], 200);
}


By using `array_filter`, you can remove any keys with null values from the `$updateData` array before passing it to the `Account::update` method. This way, only the fields present in the request payload will be updated.

Rate this post

4 of 5 based on 4499 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us