public function run()
 {
     Eloquent::unguard();
     $faker = Faker::create();
     foreach (range(1, 5) as $index) {
         $contactNumber = new ContactNumber(['type' => $faker->numberBetween(1, 3), 'number' => $faker->phoneNumber]);
         $contactInfo = ContactInfo::create(['address_1' => $faker->streetAddress, 'address_2' => $faker->streetName, 'zip' => $faker->postcode, 'email' => $faker->email]);
         $contactInfo->contactNumbers()->save($contactNumber);
         $customer = Customer::create(['name' => $faker->company, 'industry_type' => $faker->numberBetween(1, 3), 'secretary_id' => $faker->numberBetween(5, 7)]);
         $customer->contactInfo()->save($contactInfo);
         $representative = CustomerRepresentative::create(['first_name' => $faker->firstName, 'last_name' => $faker->lastName, 'middle_initial' => strtoupper($faker->randomLetter), 'company_position' => 'CEO']);
         $contactInfo = ContactInfo::create(['email' => $faker->email]);
         $contactNumber = new ContactNumber(['type' => $faker->numberBetween(1, 3), 'number' => $faker->phoneNumber]);
         $contactInfo->contactNumbers()->save($contactNumber);
         $representative->contactInfo()->save($contactInfo);
         $customer->representatives()->save($representative);
     }
 }
 public function storeRepresentative($id)
 {
     $input = Input::all();
     $rules = ['first_name' => 'required', 'last_name' => 'required', 'company_position' => 'required', 'contactInfo.email' => 'required|email'];
     $validator = Validator::make($input, $rules);
     if ($validator->fails()) {
         return Redirect::back()->withErrors($validator)->withInput();
     }
     $contact = array_pull($input, 'contactInfo');
     $numbers = array_pull($contact, 'number');
     $numberTypes = array_pull($contact, 'number_type');
     $representative = CustomerRepresentative::create($input);
     $customer = Customer::find($id);
     foreach ($numbers as $index => $number) {
         $contactNumbers[] = new ContactNumber(['number' => $number, 'type' => $numberTypes[$index]]);
     }
     $newContactInfo = ContactInfo::create($contact);
     $newContactInfo->contactNumbers()->saveMany($contactNumbers);
     $representative->contactInfo()->save($newContactInfo);
     $customer->representatives()->save($representative);
     if (Session::get('redirect_to_rfq_after_create')) {
         Session::pull('redirect_to_rfq_after_create');
         return Redirect::route('quotations.create', ['customerId' => $customer->id]);
     }
     if (Session::get('redirect_to_rfq')) {
         $url = Session::pull('redirect_to_rfq');
         return Redirect::to($url)->with('message', 'Successfully added representative')->with('alert-class', 'success');
     }
     return Redirect::route('customers.edit', ['id' => $id])->with('message', 'Successfully added representative')->with('alert-class', 'success');
 }