Eloquent Relationships

Get Started. It's Free
or sign up with your email address
Rocket clouds
Eloquent Relationships by Mind Map: Eloquent Relationships

1. One to One

1.1. define one to one relationships

1.2. ->hasOne( {Eloquent Class} , 'foreign_key')

1.3. ->belongsTo( {Eloquent Class'} , 'foreign_key')

1.4. we can inserting related item: $contact->phoneNumbers()->save($phoneInstance)

2. One to Many

2.1. define one to many relationships. take example : User has many contacts

2.2. ->hasMany(Contact::class, {user_id})

2.3. $user->contacts : return a collection

2.4. $user->contacts() : return a query builder

2.5. inverse relationship: belongsTo(User::class)

2.6. if we want to attach contacts to User: $contact->user()->associate(User::first()) then we can disassociate $contact->user()->disassociate()

2.7. Selecting records that have related item

2.7.1. can select only records with specific particular criteria

2.7.2. User with contacts : User::has('contacts')->get()

2.7.3. User with contacts > 5 User::has('contacts', '>', 5)->get()

2.7.4. Nesting, User has contacts with PhoneNumbers User::has('contacts.phoneNumbers')->get()

2.8. Has many though

2.8.1. pulling relationships of relationships

2.8.2. User has many contacts and each Contacts has many PhoneNumbers

2.8.3. we can define : public function phoneNumbers(){ return $this->hasManyThrough(PhoneNumber::class, Contact::class); }

2.8.4. customize the key of intermediate class (Contact in this case) as third parameters, and distant class (PhoneNumber in this case as Fourth parameters

3. Many to Many

3.1. User has many contacts each Contacts related to many Users

3.2. belongsToMany() to define this relationship

3.3. pivot table to describe this relationship naming conventions: `users_contacts`

3.4. we must have `contact_id` and `user_id` in the pivot table

3.5. can pass id (either user_id or contact_id) to this relationship

3.6. using attach()/detach() to build relationship

3.7. updateExistingPivot() to update the pivot record

3.8. sync() : detach() all old relationship and attach() the new relationships

3.9. Getting data from Pivot Table

3.9.1. to store new fields to pivot table, define them at the relationship definitions: $this->belongsToMany()->withTimestamps() -> withPivot('field_1', 'field_2', ...)

3.9.2. access pivot record from : $user->contacts->each(function($contact){ $contact->pivot->created_at; });

4. Polymorphic

4.1. a single interface to objects of multiple types

4.2. for example: Star objects should define of which models it is relating to.

4.3. many to many polymorphic

4.3.1. example: Contacts, Events and Tags table we will have Taggables table to pivot these relationship Taggables has: tag_id, taggable_id, taggable_type (whether contacts or events)

4.3.2. morphToMany(Tag::class, 'taggable') morphedByMany(Event::class, 'taggable')

5. Child record update parent timestamps

5.1. if we want child record to update parent record, we should use this function

5.2. $touches = ['parentProperty']

6. EagerLoading

6.1. Laravel dont pull the relationship (Lazy Loading)

6.2. We can eager loading relationship with keywords `with`

6.3. multiple eager: Contact::with('users', 'addresses')->get()

6.4. or constraining eagerloading Contact::with(['addresses' =>function($query){ $query->where('mailable', true); }])->get();

6.5. use $contacts->load('phoneNumbers') to lazy eager loading

6.6. eagerloading just the count : withCount('posts')