Laravel eloquent cheatsheet

One to One

User ─── hasOne ───> Profile

Profile ─── belongsTo ───> User

Profiles.user_id (FK).

One to Many

School ─── hasMany ───> Student

Student ─── belongsTo ───> School

students.school_id (FK).

Many to Many

Post ─── belongsToMany ───> Tag

Tag ─── belongsToMany ───> Post

Pivot table post_tag (post_id, tag_id).

One to One Through

Country ─── hasOneThrough ───> Post

Country ───> User ───> Post

A Country has one Post through a User.

One to Many Through

Country ─── hasManyThrough ───> Post

Country ───> User ───> Post

A Country has many Posts through Users.

 

 Memory Formula

FK side = belongsTo

Other side = hasOne / hasMany

Pivot = belongsToMany (both sides)

Through = skip middle model (hasOneThrough / hasManyThrough)

No comments

Powered by Blogger.