Laravel9 ORM Relationships

  1. One to one
  2. One to many
  3. Many to many
  4. Has one through
  5. Has many through
  6. One to one (Polymorphic)
  7. One to many (Polymorphic)
  8. Many to many (Polymorphic)
 

One to one

 
A user can have a phone model
 
At user model call phone method and this method contain hasOne method.
 
public function phone(){
return $this->hasOne(Phone::class);
}
Note:
Once relation has established, we can fetch related record.
 
$phone = User::find(1)->phone;

Override foreign key.

public function phone(){
return $this->hasOne(Phone::class, foreign_key);
}

Override local key

public function phone(){
return $this->hasOne(Phone::class, foreign_key, local_key);
}
Note:
foreign key will found at child table where local key will be a column at parent table by default it is id column.
 

Inverse of one to one relationships.

In this example, a phone is belongs to the user.
there will be user method and it will contain belongsTo() method which will point the parent table.
 
public function user(){
return $this->belongsTo(User::class);
}
or
public function user(){
return $this->belongsTo(User::class, foreign_key, local_key);
}
Note:
foreign key will found at child table where local key will be a column at parent table by default it is id column.
 

One to many

A post table may have an infinite number of comments.
 
post model will contain comments method and this method will contain hasMany method.
 
class Post extends Model{
public function comments(){
return $this->hasMany(Comment::class, foreign_key, local_key);
}
}
Note:
Post::find(2)->comments;

One to many inverse

every comment belongs to post
Here Comment model will contain the method post and this will contain belongsTo method.
 
class Comment extends Model{
public function post(){
return $this->belongsTo(Post::class, foreign_key, local_key);
}
}

Has one through

Suppose we have database schema.
 
There is three models Cars, Mechanics, Owners.
Where Mechanics belongs to Cars and every cars belongs to owners.
Some time mechanics need to access information about owner of car.
 
class Mechanic extends Model
{
  public function carOwner()
  {
      return $this->hasOneThrough(
      Owner::class, 
      Car::class,
      'mechanic_id', // FK in car table
      'car_id', FK in owner table,
      'id', // local key in the mechanic table
      'id' // local key in the car table
      );
  }
}
Where Car::class is the intermediate table
 

Has many through

 

Many to Many Relationships

Suppose we have three models 
user
id
user_name

roles
id
role_name

user_roles
id
user_id
role_id
 
Here user can have multiple roles like(author, editor) and every role may belongs to multiple user.
 
Lets understand this concepts using basic asked questions.

How many roles for a user?

class User extends Model
{
  public function roles()
  {
      return $this->belongsToMany(Role::class);
  }
}

How many users have roles?

class Role extends Model
{
  public function users()
  {
      return $this->belongsToMany(User::class);
  }
}

When a role assigned to the user?

echo $role->pivot->created_at;
 
class User extends Model
{
  public function roles()
  {
      return $this->belongsToMany(Role::class)->wherePivot("approved", 1);
  }
}

Polymorphic relationships

One to One(Polymorphic)

 
The child model can belongs to more than one model
 
posts
  id - integer
    name - string

users
  id - integer
    name - string

images
  id - integer
  url - string
  imageable_id - integer
    imageable_type - string
 
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
  /**
   * Get the parent imageable model (user or post).
   */
  public function imageable()
  {
      return $this->morphTo();
  }
}

class Post extends Model
{
  /**
   * Get the post's image.
   */
  public function image()
  {
      return $this->morphOne(Image::class, 'imageable');
  }
}


class User extends Model
{
  /**
   * Get the user's image.
   */
  public function image()
  {
      return $this->morphOne(Image::class, 'imageable');
  }
}

One to Many (Polymorphic)

Posts
id
post


Videos
id
video

comments
id
body
commentable_id
commentable_type
 

Many to Many (Polymorphic)

Posts, videos Model can share Tag Model
© 2016 - 2022, All Rights are Reserved.