Skip to content

Instantly share code, notes, and snippets.

@jrmessias
Created August 9, 2018 19:53
Show Gist options
  • Select an option

  • Save jrmessias/61d8c3c6193a4281fcfd0a4451ad73c5 to your computer and use it in GitHub Desktop.

Select an option

Save jrmessias/61d8c3c6193a4281fcfd0a4451ad73c5 to your computer and use it in GitHub Desktop.
Laravel table relationships
* hasOne / hasMany (1-1, 1-M)
-save(new or existing child)
-saveMany(array of models new or existing)
-create(array of attributes)
-createMany(array of arrays of attributes)
---------------------------------------------------------------------------
* belongsTo (M-1, 1-1)
-associate(existing model)
---------------------------------------------------------------------------
* belongsToMany (M-M)
-save(new or existing model, array of pivot data, touch parent = true)
-saveMany(array of new or existing model, array of arrays with pivot ata)
-create(attributes, array of pivot data, touch parent = true)
-createMany(array of arrays of attributes, array of arrays with pivot data)
-attach(existing model / id, array of pivot data, touch parent = true)
-sync(array of ids OR ids as keys and array of pivot data as values, detach = true)
-updateExistingPivot(relatedId, array of pivot data, touch)
---------------------------------------------------------------------------
* morphTo (polymorphic M-1)
// the same as belongsTo
---------------------------------------------------------------------------
* morphOne / morphMany (polymorphic 1-M)
// the same as hasOne / hasMany
---------------------------------------------------------------------------
* morphedToMany /morphedByMany (polymorphic M-M)
// the same as belongsToMany
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment