Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Person:
- **
- * One-to-many relationship to ContactInfo
- *
- * @return IlluminateDatabaseEloquentRelationshasMany
- */
- public function contactInfo()
- {
- return $this->hasMany(ContactInfo::class);
- }
- ContactInfo:
- /**
- * One-to-many polymorphic relationship to Contact types (Email, Phone, Address, etc.)
- *
- * @return IlluminateDatabaseEloquentRelationsmorphTo
- */
- public function contactable()
- {
- return $this->morphTo();
- }
- Example Email:
- /**
- * Many-to-one polymorphic relationship to Person
- *
- * @return IlluminateDatabaseEloquentRelationsmorphMany
- */
- public function contactInfo()
- {
- return $this->morphMany('AppModelsContactInfo', 'contactable');
- }
- Schema::create(
- 'contact_info', function (Blueprint $table) {
- $table->increments('id');
- $table->uuid('person_id');
- $table->index('person_id');
- $table->foreign('person_id')->references('id')->on('people');
- $table->morphs('contactable');
- $table->boolean('is_primary')->default(false);
- $table->boolean('is_active')->default(true);
- $table->timestamps();
- $table->softDeletes();
- }
- );
- $person = AppModelsPerson::create($request->person);
- $contactInfo = new AppModelsContactInfo();
- $contactInfo->person_id = $person->id;
- $contactInfo->contactable()->attach(Email::create(['email' => $request->contactInfo['email']]));
- $contactInfo->contactable()->attach(Phone::create(['number' => $request->contactInfo['homePhone'], 'type' => 'Home']));
- $contactInfo->contactable()->attach(Phone::create(['number' => $request->contactInfo['workPhone'], 'type' => 'Work']));
- $contactInfo->contactable()->attach(Phone::create(['number' => $request->contactInfo['fax'], 'type' => 'Fax']));
- $contactInfo->save();
Add Comment
Please, Sign In to add comment