Eloquent: Relationships

Database tables are often related to one another.

Supports a variety of common relationships:

  • One to one relationship

  • The inverse of a one to one relationship

  • One to many relationship

  • The inverse of a one to many relationship

  • Many to many relationship

One To One

A one-to-one relationship is a very basic type of database relationship.

For example, a User model might be associated with one Phone model.

To define this relationship, we will place a phone method on the Users model.

The phone method should call the hasOne method and return its result.

class User extends Model implements ModelBuild {

    /*
    * Class
    *
    */
    protected static $__CLASS__ = __CLASS__; 

    /*
    * Table
    *
    */
    public $table = "users"; 

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

The first argument passed to the hasOne method is the name of the related model class. Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. Dynamic properties allow you to access relationship methods as if they were properties defined on the model:

$phone = User::find(1)->phone();

Parameters:

# Parameters
hasOne($table, $foreign_key, $primary_key);

#Example
DB::table("users")->get()->hasOne("posts", "user_id", "id");

The inverse of a one to one relationship

So, we can access the Phone model from our User model. Next, let's define a relationship on the Phone model that will let us access the user that owns the phone. We can define the inverse of a hasOne relationship using the belongOnemethod:

class Phones extends Model implements ModelBuild {

    /*
    * Class
    *
    */
    protected static $__CLASS__ = __CLASS__; 

    /*
    * Table
    *
    */
    public $table = "phone"; 

    /*
    *
    */
    public function user()
    {
       return $this->belongOne(User::class);
    }
    
}

Parameters:

belongOne($table, $foreign_key, $primary_key)

#Example
DB::table("phones")->get()->belongOne("users", "user_id", "id");

One to many relationship

A one-to-many relationship is used to define relationships where a single model is the parent to one or more child models. For example, a blog post may have an infinite number of comments. Like all other Eloquent relationships, one-to-many relationships are defined by defining a method on your Eloquent model:

class Post extends Model implements ModelBuild {

    /*
    * Class
    *
    */
    protected static $__CLASS__ = __CLASS__; 

    /*
    * Table
    *
    */
    public $table = "posts"; 

    /*
    *
    */
    public function comments()
    {
        return $this->has(Comment::class);
    }

}

Parameters:

has($table, $foreign_key, $primary_key)

#Example
DB::table("posts")->get()->has("comments", "comment_id", "id");

The inverse of a one to many relationship

use belong() method for the inverse of a one to many relationship.

class Comment extends Model implements ModelBuild {

    /*
    * Class
    *
    */
    protected static $__CLASS__ = __CLASS__; 

    /*
    * Table
    *
    */
    public $table = "comments"; 

    /*
    *
    */
    public function Posts()
    {
        return $this->belong(Post::class);
    }

}

Parameters:

belong($table, $foreign_key, $primary_key)

#Example
DB::table("comments")->get()->belong("posts", "comment_id", "id");

Many To Many Relationship

Many to many relations are slightly more complicated than hasOne and has relationships.

use have() method.

Table Structure

users
    id - integer
    name - string
 
products
    id - integer
    name - string
 
product_user
    id - integer
    user_id - integer
    product_id - integer

Model Structure

class User extends Model implements ModelBuild {

    /*
    * Class
    *
    */
    protected static $__CLASS__ = __CLASS__; 

    /*
    * Table
    *
    */
    public $table = "users"; 

    /*
    *
    */
    public function products()
    {
        return $this->have(Product::class, "product_user");
    }

}

Parameters:

have($table, $linked_table, $table_foreign_key, $foreign_key, $table_primary_key, $primary_key)

#Example
return $this->title("myProductsUsers")->haveTitle("myProducts")->have("products", "product_user", "product_id", "user_id", 'id', 'id');

Title of relationships

You can specify the title of the relationship using the method title().

You can specify the name of the child relationship using the method haveTitle().

return $this->title("myProductsUsers")->haveTitle("myProducts")->have("products", "product_user");

Multiple relationships

You can use more than one relation.

DB::table("users")->get()->has("comments", "user_id")->hasOne("posts", "user_id")

Last updated