/** * store Contact * * @param Business $business Business that holds the Contact * @param AlterContactRequest $request Alter Contact Request * @return Response View for created Contact * or Redirect */ public function store(Business $business, AlterContactRequest $request) { $this->log->info(__METHOD__); $business_name = $business->name; Notifynder::category('user.subscribedBusiness')->from('App\\Models\\User', auth()->user()->id)->to('App\\Models\\Business', $business->id)->url('http://localhost')->extra(compact('business_name'))->send(); $existing_contacts = Contact::whereNull('user_id')->whereNotNull('email')->where('email', '<>', '')->where(['email' => $request->input('email')])->get(); foreach ($existing_contacts as $existing_contact) { if ($existing_contact->isSubscribedTo($business)) { auth()->user()->contacts()->save($existing_contact); Flash::warning(trans('user.contacts.msg.store.warning.already_registered')); return redirect()->route('user.business.contact.show', [$business, $existing_contact]); } $business->contacts()->attach($existing_contact); $business->save(); Flash::warning(trans('user.contacts.msg.store.warning.showing_existing_contact')); return redirect()->route('user.business.contact.show', [$business, $existing_contact]); } $contact = Contact::create(Request::all()); $contact->user()->associate(auth()->user()->id); $contact->save(); $business->contacts()->attach($contact); $business->save(); event(new NewRegisteredContact($contact)); Flash::success(trans('user.contacts.msg.store.success')); return redirect()->route('user.business.contact.show', [$business, $contact]); }
/** * Find an existing Contact By Email. * * @param string $email * * @return Collection|Builder */ protected function findExistingContactsByEmail($email) { return Contact::whereNull('user_id')->whereNotNull('email')->where('email', '<>', '')->where(['email' => $email])->get(); }