Friendly Reference to Eloquent Relations

Submitted by Mahmoud_Zalt - 2 years ago

This is my personal reference.

/**
in One to One:
u have to use HasOne on the first model and BelongsTo on the second model

to add record on the first model (HasOne) use the save function
example:    $post->comments()->save($comment);

to add record on the second model (BelongsTo) use the associate function
example:    $user->account()->associate($account);    $user->save();




in One to Many:
u have to use HasMany on the first model and BelongsTo on the second model

to add record on the first table (HasMany) use the save or saveMany functions
example:    $post->comments()->saveMany($comments);

to add record on the second model (BelongsTo) use the associate function
example:    $user->account()->associate($account);    $user->save();




in Many to Many:
u have to use BelongsToMany on the first model and BelongsToMany on the second model

to add records on the pivot table use attach or sync functions
- both functions accepts single ID or array of ID’s 
- the difference is attach checks if the record already exist on the pivot table while sync don’t




in Polymorphic One to Many:
u have to use MorphMany on the main model and MorphTo on all the (***able) models


to add records on all the other models use the save
example:    $course->tags()->save($tag);

the pivot table should have the following columns:
. main model ID
. (***able) ID
. (***able) Type




in Polymorphic Many to Many:
u have to use MorphByMany on the main model and MorphToMany on all the (***able) models

to add records on all the other models use the save or saveMany
example:    $course->tags()->save($tag);
example:    $course->tags()->saveMany([$tag_1, $tag_2, $tag_3]);

the pivot table should have the following columns:
. main model ID
. (***able) ID
. (***able) Type




in Has Many Through (shortcut):
u have to use HasManyThrough on the first table and have the normal relations on the other 2 tables

this doesn’t work for ManyToMany relationships (where there’s a pivot table)
however there’s a nice and easy solution {check out another note}


*/

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks