function add(array $request) { DB::beginTransaction(); try { if ($request['ex_clients_id'] == 0) { $client = Client::create(['preferred_name' => $request['preferred_name'], 'title' => $request['title'], 'given_name' => $request['given_name'], 'surname' => $request['surname'], 'added_by_users_id' => \Auth::user()->id]); $phone_number = $request['phone']; $phone = Phone::create(['number' => $phone_number, 'type' => 'home']); ClientPhone::create(['phones_id' => $phone->id, 'ex_clients_id' => $client->id, 'is_current' => 1]); $client_id = $client->id; } else { $client_id = $request['ex_clients_id']; } $lead = Lead::create(['referral_notes' => $request['referral_notes'], 'status' => 0, 'ex_clients_id' => $client_id, 'added_by_users_id' => \Auth::user()->id]); $loan = Loan::create(['loan_purpose' => $request['loan_purpose'], 'loan_type' => $request['loan_type'], 'ex_leads_id' => $lead->id]); //add logs $log = Log::create(['added_by' => \Auth::user()->id, 'comment' => 'Lead Created']); LeadLog::create(['ex_lead_id' => $lead->id, 'log_id' => $log->id]); DB::commit(); return $lead->id; } catch (\Exception $e) { DB::rollback(); dd($e); return false; } }
function addPhoneDetails($request, $key, $applicant_id) { //if(isset($request['mobile'][$key]) && $request['mobile'][$key] != null) { we need it for later in edit box $mobile_phone = Phone::create(['number' => $request['mobile'][$key], 'type' => 'mobile']); ApplicantPhone::create(['phones_id' => $mobile_phone->id, 'applicants_id' => $applicant_id]); //} //if(isset($request['home'][$key]) && $request['home'][$key] != null) { $home_phone = Phone::create(['number' => $request['home'][$key], 'type' => 'home']); ApplicantPhone::create(['phones_id' => $home_phone->id, 'applicants_id' => $applicant_id]); //} //if(isset($request['work'][$key]) && $request['work'][$key] != null) { $work_phone = Phone::create(['number' => $request['work'][$key], 'type' => 'work']); ApplicantPhone::create(['phones_id' => $work_phone->id, 'applicants_id' => $applicant_id]); //} }
function remove($client_id) { DB::beginTransaction(); try { //get details $client = Client::find($client_id); //change this later for multiple addresses $client_address = ClientAddress::where('ex_clients_id', $client->id)->first(); $address = Addresses::find($client_address->address_id); //phone numbers $all_client_phones = ClientPhone::where('ex_clients_id', $client->id); $client_phones = $all_client_phones->get(); $all_client_phones->delete(); /* delete data */ //delete phone numbers if (!empty($client_phones)) { foreach ($client_phones as $client_phone) { $phones = Phone::find($client_phone->phones_id)->delete(); } } //delete address $client_address->delete(); $address->delete(); //delete client $client->delete(); DB::commit(); // all good } catch (\Exception $e) { DB::rollback(); dd($e); // something went wrong } }