Current File : //home/tradevaly/www/app/Http/Controllers/Frontend/ContactController.php
<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Contact;
use App\Models\Help;
use App\Models\User;
use App\Models\Company;
use Auth;
use App\Models\Banner;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Mail;
use Carbon\Carbon;
use Illuminate\Support\Facades\Notification;
use App\Notifications\UserVerification;
use App\Notifications\UserRegister;

class ContactController extends Controller
{
    public function contact(Request $request){
       
        if($request->isMethod('post')){
           if(User::where('email', $request->email)->first() != null){
             Toastr::error('Email Already Exists', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return redirect()->back();
        }
        if(User::where('username', $request->email)->first() != null){
             Toastr::error('Username Already Exists', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return redirect()->back();
        }
        
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required',
            'password' => 'required',
            'category_id' => 'required',
            'role' => 'required',
            'country_id' => 'required',
            'company' => 'required',
            'g-recaptcha-response' => 'required',
        ]);
        $password = $request->password;
        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->category_id = $request->category_id;
        $user->username = $request->username;
        $user->type = $request->role;
        $user->image = 'image.jpg';
        $user->password = bcrypt($password);
        $user->verify_code = rand(999,9999);
        $user->country_id = $request->country_id;
        $user->package_id = 1;
        $user->next_pay = Carbon::now()->addYear(10);
        $user->save();
        if($user->save()){
            $user->password = $password;
            $company = new Company();
            $company->user_id = $user->id;
            $company->name = $request->company;
            $company->image = 'image.jpg';
            $company->phone = $request->phone;
            if($company->save()){
                Notification::send($user, new UserRegister($user));
                Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new UserRegister($user));
                Notification::send($user, new UserVerification($user));
                 if($request->admin_user_create){
                         Toastr::success('Registration Successfull');
                      return back();
                    }

                Auth::loginUsingId($user->id);
                Toastr::success('Registration Successfull Please Check Your Email !! For Login', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return view('thankyou');
            }
        }
        }
    }
    public function view($id){
        $massage=Contact::find($id);
        $msgs=Contact::all();
       return view('backend.pages.massage')->with(compact('massage','msgs'));
    }
    public function delete($id){
        $massage=Contact::find($id)->delete();
        Toastr::success('Deleted!','Got it');
        return redirect('b2b/dashboard');
       return view('backend.pages.massage')->with(compact('massage','msgs'));
    }
    
    // Contact us section
     public function help(Request $request){
       
        $info= new Help();
        if($request->isMethod('post')){    
              $request->validate([
             'f_name' => 'required',
             'l_name' => 'required',
             'u_name' => 'required',
             'phone' => 'required',
             'email' => 'required',
             'c_name' => 'required',
             'message' => 'required',
             'g-recaptcha-response' => 'required|captcha',
             'check' => 'required',
             ]);
            $info->f_name=$request['f_name'];
            $info->l_name=$request['l_name'];
            $info->u_name=$request['u_name'];
            $info->phone=$request['phone'];
            $info->email=$request['email'];
            $info->c_name=$request['c_name'];
            $info->message=$request['message'];
            if(empty($request['f_name']) || empty($request['l_name']) || empty($request['u_name']) || empty($request['email']) || empty($request['message']) || empty($request['phone'])){
             Toastr::error('One Or More Required Field Left Empty!','Got it');
             return redirect()->back();
            }
            $info->save();
           
          Mail::to('tradevalyb2b@gmail.com')->send(new \App\Mail\ContactMessage($info));
            Toastr::success('We will response soon!','Got it');
             return redirect()->back();
        }
        $data['banners'] = Banner::where('position','contact-banner')->latest()->get();
       return view('frontend.pages.contact')->with($data);
    }
    public function index(){
        $data['helps']=Help::latest()->get();
       return view('backend.pages.help.index')->with($data);
    }
    public function show($id){
        $help=Help::findOrFail($id);
        
       return view('backend.pages.help.show')->with(compact('help'));
    }
   
    public function c_delete($id){
        $massage=Help::find($id)->delete();
        Toastr::success('Deleted Successfully!','Success.');
        return back();
    }
}