Guest User

Untitled

a guest
Feb 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. Person:
  2. **
  3. * One-to-many relationship to ContactInfo
  4. *
  5. * @return IlluminateDatabaseEloquentRelationshasMany
  6. */
  7. public function contactInfo()
  8. {
  9. return $this->hasMany(ContactInfo::class);
  10. }
  11.  
  12. ContactInfo:
  13. /**
  14. * One-to-many polymorphic relationship to Contact types (Email, Phone, Address, etc.)
  15. *
  16. * @return IlluminateDatabaseEloquentRelationsmorphTo
  17. */
  18. public function contactable()
  19. {
  20. return $this->morphTo();
  21. }
  22. Example Email:
  23. /**
  24. * Many-to-one polymorphic relationship to Person
  25. *
  26. * @return IlluminateDatabaseEloquentRelationsmorphMany
  27. */
  28. public function contactInfo()
  29. {
  30. return $this->morphMany('AppModelsContactInfo', 'contactable');
  31. }
  32.  
  33. Schema::create(
  34. 'contact_info', function (Blueprint $table) {
  35. $table->increments('id');
  36. $table->uuid('person_id');
  37. $table->index('person_id');
  38. $table->foreign('person_id')->references('id')->on('people');
  39. $table->morphs('contactable');
  40. $table->boolean('is_primary')->default(false);
  41. $table->boolean('is_active')->default(true);
  42. $table->timestamps();
  43. $table->softDeletes();
  44. }
  45. );
  46.  
  47. $person = AppModelsPerson::create($request->person);
  48. $contactInfo = new AppModelsContactInfo();
  49. $contactInfo->person_id = $person->id;
  50. $contactInfo->contactable()->attach(Email::create(['email' => $request->contactInfo['email']]));
  51. $contactInfo->contactable()->attach(Phone::create(['number' => $request->contactInfo['homePhone'], 'type' => 'Home']));
  52. $contactInfo->contactable()->attach(Phone::create(['number' => $request->contactInfo['workPhone'], 'type' => 'Work']));
  53. $contactInfo->contactable()->attach(Phone::create(['number' => $request->contactInfo['fax'], 'type' => 'Fax']));
  54. $contactInfo->save();
Add Comment
Please, Sign In to add comment