Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Apply for loop in single field in laravel

use of in_array and explode
Apply for loop in single field
Apply for loop in whole table
Input

Image description

IN controller

 public function addcarts(Request $request)
    {

        Log::info('In addcarts store');
        Log::info('Data received via AJAX:', $request->all());
        $itemId = $request->input('dataitem_id');
}
Enter fullscreen mode Exit fullscreen mode

 Data received via AJAX: {"_token":"uJFyM5MacvkbIQOn8Jiic8QOztkm0oKctzHhyW0Z","admin_id":"28","slug":"Amit","influencer_email":"abc@gmail.com","admin_email":"rakeshdev.cotocus@gmail.com","user_name":"rakesh","item":"24","dataitem_id":"24","selected_socials":"face_price,youtube_price"} 
// Define an array of social sites
$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'];


Enter fullscreen mode Exit fullscreen mode
   public function addcarts(Request $request)
    {

        Log::info('In addcarts store');
        Log::info('Data received via AJAX:', $request->all());
        $itemId = $request->input('dataitem_id');
            Log::info($itemId);
            $input = $request->all();
            $cartID = null;
            $admins_id=  $request->input('admin_id');
            $admins_email=  $request->input('admin_email');
            $user_name=  $request->input('user_name');
            $addcart= addcart::where('admin_id', $admins_id)
            ->orderBy('created_at', 'desc') // Order by 'created_at' column in descending order
            ->get();   
             $addprofile=Addprofile::where('id',$itemId)->first();
             Log::info($addprofile);

             $payments=payments::where('admin_id',$addprofile->user_id)->first();
             Log::info($payments);           
             $slug= $addprofile->slug;
             $influencer_email= $addprofile->user_email;
             $admin_email=  $request->input('admin_email');
             $user_name=  $request->input('user_name');
             $influencer_admin_id= $addprofile->user_id;

           if(!$addcart->isEmpty())
           {
            $carts_id =$addcart->pluck('cart_id')->toArray();
            $pays=  Paytm::where('cart_id', $carts_id[0])->where('admin_id',$admins_id)->get();
                        if (!$pays->isEmpty())
                        {
                            $cartsID = 'CART-' . Str::random(4);    
                            $cartID = $cartsID. '-' . $admins_id; 
                            Log::info($cartID);                  
                        }
                        else{
                            $addcarts_id =$addcart->pluck('cart_id')->toArray();
                            $cartID = $addcarts_id[0];
                            Log::info($cartID); 
                        }
                    }
                    else{
                        Log::info("last cart id hain na"); 
                        $cartsID = 'CART-' . Str::random(4);    
                        $cartID = $cartsID. '-' . $admins_id;
                        }

                if($addprofile) 
                { 
                    Log::info("addprofile cart id hain na"); 
                    $selectedSocials = explode(',', $request->selected_socials);
                    $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'];
                 // Create a single instance of addcart
                        $addCart = new addcart;
                        $addCart->admin_id = $admins_id;
                        $addCart->cart_id = $cartID;
                        $addCart->influencer_admin_id = $addprofile->user_id;
                        $addCart->admin_email = $admins_email;
                        $addCart->user_name = $user_name;
                        $addCart->slug = $slug;
                        $addCart->influencer_email = $influencer_email;

                        foreach ($socialSites as $socialSite) {
                            // Check if the social site is in $selectedSocials
                            if (in_array($socialSite, $selectedSocials)) {
                                // Update the property of the existing $addCart instance
                                $addCart->{$socialSite} = $payments->{$socialSite} ?? 0;
                            }
                        }

                        // Save the $addCart instance after the loop
                        $addCart->save();


                    log::info($itemId);
                     $addingprofile= DB::table("addprofiles")->where('id',$itemId)->update(['addcarts' => "$admins_email:yes"]);
                    return response()->json([
                        'message' => "task disapprove successfully", 
                        'success' => true,
                    ]);
                  }
        }
Enter fullscreen mode Exit fullscreen mode

Explanation:

Explode Socials: The code splits a comma-separated string of selected socials ($request->selected_socials) into an array ($selectedSocials).

Object Initialization: An instance of the addcart class is created, and various properties of this instance are set based on different variables.

Loop through Social Sites: It iterates through each social site in the predefined array ($socialSites).

Update Instance Properties: For each social site, it checks if the site is in the selected socials array. If true, it updates the corresponding property of the $addCart instance with the payment value from the $payments object, or defaults to 0 if the property is not set.

Save to Database: Finally, the updated $addCart instance is saved to the database.
output
In php my admin

Image description

Image description

Apply for loop in whole table

 $selectedSocials = explode(',', $request->selected_socials);
                    $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'];
                    foreach ($socialSites as $socialSite) {
                        // Check if the social site is in $selectedSocials
                        if (in_array($socialSite, $selectedSocials)) {
                              $addCart = new addcart;
                               $addCart->admin_id = $admins_id;
                                $addCart->cart_id = $cartID;
                                $addCart->influencer_admin_id = $addprofile->user_id;
                                $addCart->admin_email = $admins_email;
                                $addCart->user_name = $user_name;
                                 $addCart->slug = $slug;
                                $addCart->influencer_email = $influencer_email; 
                                 $addCart->{$socialSite} = $payments->{$socialSite} ?? 0;
                                 $addCart->save();
                        }
                    }
Enter fullscreen mode Exit fullscreen mode

SUMMARY

use of in_array and explode
Apply for loop in single field
Apply for loop in whole table
take log info request->all
declare cartd id as null to define globally
how to get single value from col
3 way
first ---> col name
values
pluck-->toarray-->data[0]
how to apply in_array for dynamic request string
we need two different array as parameter for in_array
convert dynamic request to array using explode
take another array as hardcode or pluck-->toarray from db
then apply in_array

handling nullvalue using ternary opertor inside for loop while saving dynamic variable $addCart->{$socialSite}
how to insert two words seprated by colon
Apply for loop in whole table
convert given dynamic request to array using in_array and take another array by hardcode
apply foreach loop
then apply inarry if condition
then create object of modal
then save multiple field
Apply for loop in single field
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 inarry if condition
then save single field

Top comments (0)