/** * Store a newly created resource in storage. * * @return Response */ public function store() { $uId = \Auth::user()->id; // User details // Transaction begins here \DB::beginTransaction(); $displayname = \Input::get('displayname'); $email = \Input::get('email'); $username = \Input::get('company_code'); $password = str_random(10); $hashPassword = \Hash::make($password); $user = \User::insertGetId(array('displayname' => $displayname, 'email' => $email, 'username' => $username, 'password' => $hashPassword, 'profilesId' => 3, 'active' => 'Y')); if (!$user) { \DB::rollback(); return \Redirect::back()->with('error', 'Failed to add'); } // User contact $mobile = \Input::get('mobile'); $alt_mobile = \Input::get('alt_mobile'); $alt_email = \Input::get('alt_email'); //Insert data to user contact table $contact = \UserContact::insert(array('mobile' => $mobile, 'alt_mobile' => $alt_mobile, 'alt_email' => $alt_email)); // insert frends table $friends = \Friends::insert(array('child_id' => $user, 'parent_id' => $uId, 'profilesId' => 3)); if (!$friends) { \DB::rollback(); return \Redirect::back()->with('error', 'Failed to add'); } //Company info $company_name = \Input::get('company_name'); $company_address = \Input::get('company_address'); $company_city = \Input::get('company_city'); $company_state = \Input::get('company_state'); $company_pin = \Input::get('company_pin'); $company_land_line = \Input::get('company_land_line'); $company_alt_land_line = \Input::get('company_alt_land_line'); $company_fax = \Input::get('company_fax'); $company_website = \Input::get('company_website'); //Insert data to company table $company = \Company::insertGetId(array('company_name' => $company_name, 'company_address' => $company_address, 'company_city' => $company_city, 'company_state' => $company_state, 'company_pin' => $company_pin, 'company_phone' => $company_land_line, 'company_alt_phone' => $company_alt_land_line, 'company_fax' => $company_fax, 'company_website' => $company_website, 'user_id' => $user)); // if company not inserted the rollback if (!$company) { \DB::rollback(); return \Redirect::back()->with('error', 'Failed to add'); } elseif ($company) { \DB::commit(); \Mail::send('emails.user_credential', array('name' => $displayname, 'username' => $username, 'password' => $password), function ($message) use($email, $username) { $message->to($email, $username)->subject('User Credential'); }); return \Redirect::route('branch.client.index')->with('success', 'Successfully added'); } }