Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Updating and Inserting Records in json array of object with Laravel

Difference between merging of two array and 3 array

using array_merge how to comine existing and new data

step1: first retrieves data that matching condition

  $socialurl=DB::connection('payments')->table("social_url")->where('id',$itemId)->first();
  $addingcarts = DB::connection('payments')->table('addcarts')->where('influencer_admin_id', $socialurl->user_id)->first();
Enter fullscreen mode Exit fullscreen mode

step2: if condition match in both table above step then merge the exsiting data with new data for cart_socials field that contain jsaon array of object

 if ($socialurl && $addingcarts) {

                                $existingSocials = json_decode($addingcarts->cart_socials, true) ?? [];
                                Log::info("we r here");
                                Log::info($existingSocials);
                                $newSocials = json_decode(json_encode($selectedSocials), true);
                                Log::info($newSocials);
                                // Merge the existing and new socials and convert back to json
                                $mergedSocials = json_encode(array_merge($existingSocials, $newSocials));
                                Log::info($mergedSocials);
                                $addCartData = [
                                    'influencer_admin_id' => $socialurl->user_id,
                                    'admin_id' => $admins_id,
                                    'cart_id' => $cartID,
                                    'slug' => $socialurl->slug,
                                    'currency' => $socialurl->social_currency,
                                    'admin_email' => $admins_email,
                                    'user_name' => $user_name,
                                    'influencer_email' => $influencer_email,
                                    'cart_socials' =>  $mergedSocials,
                                    'social_site_id' => json_encode($datasocial),

                                ];

                                // Use updateOrInsert with both conditions
                                addcart::updateOrInsert(
                                    [
                                        'id' => $itemId, 
                                        'influencer_admin_id' => $socialurl->user_id,
                                    ],
                                    $addCartData
                                );
                            } 
Enter fullscreen mode Exit fullscreen mode

step3 if condition not match in both table above step insert new data that contain jsaon array of object

else {

                                $addCartData = [
                                    'influencer_admin_id' => $socialurl->user_id,
                                    'admin_id' => $admins_id,
                                    'cart_id' => $cartID,
                                    'slug' => $socialurl->slug,
                                    'admin_email' => $admins_email,
                                    'currency' => $socialurl->social_currency,
                                    'user_name' => $user_name,
                                    'influencer_email' => $influencer_email,
                                    'cart_socials' => json_encode($selectedSocials),
                                    'social_site_id' => json_encode($datasocial),

                                ];

                                // Use updateOrInsert with both conditions
                                addcart::updateOrInsert(
                                    [
                                        'id' => $itemId, 
                                        'influencer_admin_id' => $socialurl->user_id,
                                    ],
                                    $addCartData
                                );
                            }
Enter fullscreen mode Exit fullscreen mode

*full code *

  $socialurl=DB::connection('payments')->table("social_url")->where('id',$itemId)->first();
  $addingcarts = DB::connection('payments')->table('addcarts')->where('influencer_admin_id', $socialurl->user_id)->first();

                            if ($socialurl && $addingcarts) {

                                $existingSocials = json_decode($addingcarts->cart_socials, true) ?? [];
                                Log::info("we r here");
                                Log::info($existingSocials);
                                $newSocials = json_decode(json_encode($selectedSocials), true);
                                Log::info($newSocials);
                                // Merge the existing and new socials and convert back to json
                                $mergedSocials = json_encode(array_merge($existingSocials, $newSocials));
                                Log::info($mergedSocials);
                                $addCartData = [
                                    'influencer_admin_id' => $socialurl->user_id,
                                    'admin_id' => $admins_id,
                                    'cart_id' => $cartID,
                                    'slug' => $socialurl->slug,
                                    'currency' => $socialurl->social_currency,
                                    'admin_email' => $admins_email,
                                    'user_name' => $user_name,
                                    'influencer_email' => $influencer_email,
                                    'cart_socials' =>  $mergedSocials,
                                    'social_site_id' => json_encode($datasocial),

                                ];

                                // Use updateOrInsert with both conditions
                                addcart::updateOrInsert(
                                    [
                                        'id' => $itemId, 
                                        'influencer_admin_id' => $socialurl->user_id,
                                    ],
                                    $addCartData
                                );
                            } else {

                                $addCartData = [
                                    'influencer_admin_id' => $socialurl->user_id,
                                    'admin_id' => $admins_id,
                                    'cart_id' => $cartID,
                                    'slug' => $socialurl->slug,
                                    'admin_email' => $admins_email,
                                    'currency' => $socialurl->social_currency,
                                    'user_name' => $user_name,
                                    'influencer_email' => $influencer_email,
                                    'cart_socials' => json_encode($selectedSocials),
                                    'social_site_id' => json_encode($datasocial),

                                ];

                                // Use updateOrInsert with both conditions
                                addcart::updateOrInsert(
                                    [
                                        'id' => $itemId, 
                                        'influencer_admin_id' => $socialurl->user_id,
                                    ],
                                    $addCartData
                                );
                            }
Enter fullscreen mode Exit fullscreen mode

Difference between merging of two array and 3 array

Merging of two array

[2024-02-29 00:34:14] local.INFO: array (
  'facebook' => '24',
)  
[2024-02-29 00:34:14] local.INFO: array (
  'twitter' => '67',
) 
Enter fullscreen mode Exit fullscreen mode

Final Output

[2024-02-29 00:34:14] local.INFO: {"facebook":"24","twitter":"67"} 
Enter fullscreen mode Exit fullscreen mode
   $existingSocials = json_decode($addingcarts->cart_socials, true) ?? [];
                                Log::info("we r else1 here");
                                Log::info($existingSocials);
                                $newSocials = json_decode(json_encode($selectedSocials), true);
                                Log::info($newSocials);                               
                                $mergedSocials = json_encode(array_merge($existingSocials, $newSocials));
                                Log::info($mergedSocials);  
Enter fullscreen mode Exit fullscreen mode

Merging of three array

 $mycarts = DB::connection('payments')->table('addcarts')->where('paid', "yes")->where('influencer_admin_id', $socialurl->user_id)->first();
                                Log::info("we r paydatas else here");
                                Log::info($paydata->cart_socials);
                                $existingSocials = json_decode($mycarts->cart_socials, true) ?? [];
                                Log::info("we r else here");
                                Log::info($existingSocials);
                                $allreadyexistingSocials = json_decode($addingcarts->cart_socials, true) ?? [];
                                Log::info($allreadyexistingSocials);
                                $newSocials = json_decode(json_encode($selectedSocials), true);
                                Log::info($newSocials);                               
                                $mergedSocials = json_encode(array_merge($existingSocials,$allreadyexistingSocials, $newSocials));
                                Log::info($mergedSocials); 
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

[2024-02-29 00:28:05] local.INFO: array (
  'facebook' => '346',
)  
[2024-02-29 00:28:05] local.INFO: array (
  'facebook' => '346',
  'twitter' => '100',
)  
[2024-02-29 00:28:05] local.INFO: array (
  'youtube' => '60',
)  
Enter fullscreen mode Exit fullscreen mode

*Final merging *

{"facebook":"346","twitter":"100","youtube":"60"}  
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)