my request data coming from fron end
[2024-01-05 07:46:06] local.INFO: array (
'_token' => 'JEQba4vUNZ5LP1Jz7eaI9JAwblbOAf7S8EO6i14C',
'face_price' => '24',
'youtube_price' => '69',
'admin_id' => '26',
'slug' => 'admins',
'influencer_email' => 'admins@gmail.com',
'influencer_admin_id' => NULL,
'org_name' => NULL,
'admin_email' => 'rakeshdev.cotocus@gmail.com',
'user_name' => 'rakeshdev',
'slug_id' => NULL,
)
$data= Addprofile::where('user_email',$inf_email)->first();
following below code The code will update the 'addcarts' column in the 'addprofiles' table, create a new 'addcart' instance, and update the pricing information for the selected social media platforms before saving it to the database.
if($data)
{
$addingprofile= DB::table("addprofiles")->where('id',$itemId)->update(['addcarts' => "$admins_email:yes"]);
Log::info("addprofile cart id hain na");
$selectedSocials = array_keys($request->all());
$selectedSocialsKeys = array_keys(array_flip($selectedSocials));
Log::info('Selected Socials:', ['selectedSocials' => $selectedSocialsKeys]);
$socialSites = ['face_price','twitter_price','youtube_price', 'wordpress_price', 'tumblr_price','instagram_price','quora_price', 'pintrest_price','reddit_price','koo_price','scoopit_price','slashdot_price', 'telegram_price','linkedin_price'];
$addCart = new addcart;
$addCart->admin_id =$request->admin_id;
$addCart->cart_id = $cartID;
$addCart->influencer_admin_id = $influencer_admin_id;
$addCart->admin_email = $admins_email;
$addCart->user_name = $request->user_name;
$addCart->slug = $request->user_name;
$addCart->influencer_email = $request->influencer_email;
foreach ($socialSites as $socialSite) {
Log::info("socialSites cart id hain na");
// Check if the social site is in $selectedSocials
if (in_array($socialSite, $selectedSocialsKeys)) {
Log::info("in_array cart id hain na");
// Update the property of the existing $addCart instance
$addCart->{$socialSite} = $payments->{$socialSite} ?? 0;
}
}
$addCart->save();
output
Explanation
$selectedSocials: It gets all the keys (names) from the associative array obtained from $request->all(). In this case, it's likely fetching the names of selected social media platforms from a form submission.
array_flip($selectedSocials): It flips the keys and values of the array, making the current keys (social media platform names) become values and vice versa.
array_keys(...): Finally, it retrieves the keys from the flipped array, resulting in an array with the original keys (social media platform names). This step is useful to ensure that the array contains unique values.
Summary
convert given dynamic request to array using in_array and take another array by hardcode
then create object of modal
save multiple field
apply foreach loop
then apply inarray if condition
then save single field
Top comments (0)