Debug School

rakesh kumar
rakesh kumar

Posted on

How to send mail or save data continously inside for loop if condition exist in Laravel

step 1:apply query condition using first()

  $notification= Hospital::where('mail_notifications', '1')->first();
Enter fullscreen mode Exit fullscreen mode

step 2: apply if condition after getting query result first()

 if ($notification)
Enter fullscreen mode Exit fullscreen mode

step 3: apply query inside if condition using get()

  $notification_mail= Hospital::where('mail_notifications',  '1')->get();
Enter fullscreen mode Exit fullscreen mode

step 4: apply foreach if condition exist.

foreach ( $notification_mail as $p) {
}

Enter fullscreen mode Exit fullscreen mode

step 5: send mail or save data inside foreach .

  Mail::to($userhospital['hemail'])->send(new  hospitalmail($userhospital));
                      //   Mail::send('Mail.Send_mail_user', ['data' => $quote],
Enter fullscreen mode Exit fullscreen mode
@foreach ($users as $user)
This is user {{ $user->id }}

@endforeach.
foreach ($items as $item) {
    echo $item;
}
Enter fullscreen mode Exit fullscreen mode
// Using for loop
for ($i = 0; $i < count($items); $i++) {
    echo $items[$i];
}
Enter fullscreen mode Exit fullscreen mode
public function store(Request $request)
    {
        $input = $request->all();
        $emailnot=$input['email'];
        log::info('yaha tak aa raha hai store');
       $notification= Hospital::where('mail_notifications', '1')->first();
    //    $notificationall= Hospital::get();
       log::info('yaha tak aa raha hai all'. $notification);
       if ($notification)
       {
        log::info('yaha tak aa raha hai send'. $notification);
         $notification_mail= Hospital::where('mail_notifications',  '1')->get();
          log::info('yaha tak aa raha hai send mail'. $notification_mail);
        foreach ( $notification_mail as $p) {
            $validator = Validator::make($input, [
                'name' => 'required|string|max:255',
                'country_id' => 'required|regex:/^[0-9]+$/',
                'category_id' => 'required|regex:/^[0-9]+$/',
                'email' => 'required|string|email|max:255',
                'country_code' => 'required|regex:/^\d{1,3}$/', // 'regex:/^\+\d{1,3}$/'
                'phone' => 'required|min:7|max:15',
                'patient_id' => 'required',
                'process_name' => [
                    Rule::in(['ui', 'php', 'cron', 'dbadmin'])
                ],
                'is_active' => [
                    Rule::in([0, 1])
                ],
                'status' => [
                    Rule::in(['open', 'closed', 'inprogress'])
                ],
                'desc' => 'string|max:512',
            ]);
            log::info('yaha tak'. $p);
            $categoryName = '';
            $countryName = '';
            if ($validator->fails()) {
                $response = [
                    'success' => false,
                    'data' => 'Validation Error.',
                    'message' => $validator->errors()
                ];
                return response()->json($response, 422);
            } else {
                $categoryId = intval($input['category_id']);
                $categoryName = $this->getCategoryName($categoryId);
                $countryId = intval($input['country_id']);
                $countryName = $this->getCountryName($countryId);
                if ($categoryName == 'not found') {
                    $response = [
                        'success' => false,
                        'data' => 'Validation Error.',
                        'message' => 'Category Name with the given category id: ' . $categoryId
                            . ' not found!'
                    ];
                    return response()->json($response, 422);
                } else if ($countryName == 'not found') {
                    $response = [
                        'success' => false,
                        'data' => 'Validation Error.',
                        'message' => 'Country Name with the given country id: ' . $countryId
                            . ' not found!'
                    ];
                    return response()->json($response, 422);
                }
                else {
                    Log::info('Creating Quote!');
                    //$quote = Quote::create($input);
                    $quoteNumber = $this->getQuoteCount($input['email']) + 1;
                    $quote = Quote::create([
                        'quote_no' => $quoteNumber,
                        'name' => $input['name'],
                        'country' => $countryName,
                        'category' => $categoryName,
                        'country_id' => $countryId,
                        'category_id' => $categoryId,
                        'email' => $input['email'],
                        'country_code' => $input['country_code'],
                        'phone' => $input['phone'],
                        'patient_id' => $input['patient_id'],
                        'desc' => $input['desc'],
                        'd_o_s' => (new \DateTime())->format('Y-m-d H:i:s')
                    ]);
                    $quote->is_active = $input['is_active'];
                    $quote->save();
                    Log::info('Created Quote with ID: ' . $quote);
                    $data = $quote->toArray();
                    $response = [
                        'success' => true,
                        //'data' => $data,
                        'data' =>new QuoteResource($quote),
                        'message' => 'Quote stored successfully.'
                    ];
                    $sendmail=  $p->email;
                    $userhospital = array(
                        // 'id' => $p->id,
                        'email' => $request->email,
                        'name' => $request->name,
                        'hemail' => $sendmail,
                        'subject' =>  trans('notification.activate-reset-subject'),
                        'message' =>  trans('notification.activate-reset-message'),
                        'action' =>  trans('notification.activate-reset-action'),
                      );
                      Log::info('Sending Account Activation email to: ' . $userhospital['hemail']);
                      Mail::to($userhospital['hemail'])->send(new  hospitalmail($userhospital));
                      //   Mail::send('Mail.Send_mail_user', ['data' => $quote],
                      // ==============================this email goes to admin ===============
                    //   Log::info('Sending Account Activation email to: ' . $quote['to'] . ' ...');
                    //   Mail::send('notifications.user-hospital-data', ['data' => $quote],
                    //       function ($message) use ($quote)
                    //       {
                    //           $message
                    //               // ->from(Config::get('app.MAIL_FROM'), Config::get('app.MAIL_FROM_NAME'))
                    //               ->to($quote['to'])->subject("User Activation: Welcome MyHospitalNow.com");
                    //       });
                      }
                }
            }
            return response()->json($response, 200);
       }
       // else part
       else
       {
        log::info('yaha tak aa raha hai send else');
        $validator = Validator::make($input, [
            'name' => 'required|string|max:255',
            'country_id' => 'required|regex:/^[0-9]+$/',
            'category_id' => 'required|regex:/^[0-9]+$/',
            'email' => 'required|string|email|max:255',
            'country_code' => 'required|regex:/^\d{1,3}$/', // 'regex:/^\+\d{1,3}$/'
            'phone' => 'required|min:7|max:15',
            'patient_id' => 'required',
            'process_name' => [
                Rule::in(['ui', 'php', 'cron', 'dbadmin'])
            ],
            'is_active' => [
                Rule::in([0, 1])
            ],
            'status' => [
                Rule::in(['open', 'closed', 'inprogress'])
            ],
            'desc' => 'string|max:512',
        ]);
        $categoryName = '';
        $countryName = '';
        if ($validator->fails()) {
            $response = [
                'success' => false,
                'data' => 'Validation Error.',
                'message' => $validator->errors()
            ];
            return response()->json($response, 422);
        } else {
            $categoryId = intval($input['category_id']);
            $categoryName = $this->getCategoryName($categoryId);
            $countryId = intval($input['country_id']);
            $countryName = $this->getCountryName($countryId);
            if ($categoryName == 'not found') {
                $response = [
                    'success' => false,
                    'data' => 'Validation Error.',
                    'message' => 'Category Name with the given category id: ' . $categoryId
                        . ' not found!'
                ];
                return response()->json($response, 422);
            } else if ($countryName == 'not found') {
                $response = [
                    'success' => false,
                    'data' => 'Validation Error.',
                    'message' => 'Country Name with the given country id: ' . $countryId
                        . ' not found!'
                ];
                return response()->json($response, 422);
            }
            else {
                Log::info('Creating Quote!');
                //$quote = Quote::create($input);
                $quoteNumber = $this->getQuoteCount($input['email']) + 1;
                $quote = Quote::create([
                    'quote_no' => $quoteNumber,
                    'name' => $input['name'],
                    'country' => $countryName,
                    'category' => $categoryName,
                    'country_id' => $countryId,
                    'category_id' => $categoryId,
                    'email' => $input['email'],
                    'country_code' => $input['country_code'],
                    'phone' => $input['phone'],
                    'patient_id' => $input['patient_id'],
                    'desc' => $input['desc'],
                    'd_o_s' => (new \DateTime())->format('Y-m-d H:i:s')
                ]);
                $quote->is_active = $input['is_active'];
                $quote->save();
                Log::info('Created Quote with ID: ' . $quote->id);
                $data = $quote->toArray();
                $response = [
                    'success' => true,
                    //'data' => $data,
                    'data' =>new QuoteResource($quote),
                    'message' => 'Quote stored successfully.'
                ];
                return response()->json($response, 200);
            }
        }
       }
    }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)