Skip to content

5. Relationships

DA edited this page Apr 3, 2019 · 19 revisions

In relational databases, tables are related to one another. There are several types of relationships that can be defined. This framework makes it easy to define and use relationships without having to write extensive code. There are 3 types of relationships defined in this framework: One-to-One, One-to-Many and Many-to-Many.

One-To-One Relationship

A One-to-One relationship is the simplest relationship between two entities. For example, we can have an authorization module where every User has one Account and reciprocally every Account has one User. This is a typical One-to-One relationship.

Declaration

In order to create such a relationship, you first need to make sure that the database tables that you create for the two entities both have foreign keys that link to one another.

Learn more about Relational Databases here

Once your database tables are created, one of the models User, for example, will add a new method that invokes the hasOne() method to define that relationship.

class User extends Model {

    // properties go here
    //...

    //new method that defines a One-to-One relationship
    public function account()
    {
       return $this->hasOne('Account','accounts');
       // the first argument is the model name to link to
       // the second argument is the database table name of to link to

    }
}

Inverse Relationship

In the Account model, you will need to do the same using the hasOne method.

class Account extends Model {

    // properties go here
    //...

    //new method that defines the inverse of a One-to-One relationship
    public function user()
    {
       return $this->hasOne('User','users');
       // the first argument is the model name to link to
       // the second argument is the database table name of to link to

    }
}

Notice: In both the examples, there is no restriction as to the name of the method used to define the relationships. We recommend using a name that is representative enough of the type of relationship.

Usage

After the relationship has been defined in the respective models, making use of that relationship takes place in the context of the Controller. For example, in order to get the Account associated with a User, you would do the following in the UserController:

class UserController extends Controller {

    // Assuming $user has been created
    $account = $user->find(1)->account(); // returns account for user with id 1
}

One-To-Many Relationship

A One-to-Many relationship is a common type of relationship between two entities. Taking the same example as before, you could allow one User to have multiple Account but an Account to belong to one user. For that you would want to use a One-to-Many relationship.

Declaration

In order to create such a relationship, you first need to make sure that the entity that could exist many times has a reference in the table of the other entity. In our example, that means that there should be a foreign_key in the accounts table that references the users table.

Learn more about Relational Databases here

Once your database tables are created, the User model needs a new method that invokes the hasMany() method to define that relationship.

class User extends Model {

    // properties go here
    //...

    //new method that defines a One-to-Many relationship
    public function accounts()
    {
       return $this->hasMany('Account','accounts');
       // the first argument is the model name to link to
       // the second argument is the database table name of to link to

    }
}

Inverse Relationship

In the Account model, the account belongs to one User. You will need to define a belongsTo relationship.

class Account extends Model {

    // properties go here
    //...

    //new method that defines the inverse of a One-to-One relationship
    public function user()
    {
       return $this->belongsTo('User','users');
       // the first argument is the model name to link to
       // the second argument is the database table name of to link to

    }
}

Notice: In both the examples, there is no restriction as to the name of the method used to define the relationships. We recommend using a name that is representative enough of the type of relationship.

Usage

After the relationship has been defined in the respective models, making use of that relationship takes place in the context of the Controller. For example, in order to get all the accounts associated with a User, you would do the following in the UserController:

class UserController extends Controller {

    // Assuming $user has been created
    $accounts = $user->find(1)->accounts(); // returns an Array of Accounts of all the accounts associated to user 1
}

Many-to-Many Relationship

A Many-to-Many relationship ensures that two entities could have multiple instances of each other. Taking the same example as before, you could allow one User to have multiple Account and one Account to have multiple User. For that you would want to use a Many-to-Many relationship.

Declaration

In order to create such a relationship, you do not need to do anything in the database other than making sure to follow the naming conventions.

Learn more about Naming Conventions here

The User model needs a new method that invokes the belongsToMany() method to define that relationship.

class User extends Model {

    // properties go here
    //...

    //new method that defines a Many-to-Many relationship
    public function accounts()
    {
       return $this->belongsToMany('Account','accounts');
       // the first argument is the model name to link to
       // the second argument is the database table name of to link to

    }
}

Inverse Relationship

Similarly, the Account model needs to define a belongsToMany relationship.

class Account extends Model {

    // properties go here
    //...

    //new method that defines a Many-to-Many relationship
    public function users()
    {
       return $this->belongsToMany('User','users');
       // the first argument is the model name to link to
       // the second argument is the database table name of to link to

    }
}

Notice: In both the examples, there is no restriction as to the name of the method used to define the relationships. We recommend using a name that is representative enough of the type of relationship.

Usage

After the relationship has been defined in the respective models, making use of that relationship takes place in the context of the Controller. For example, in order to get all the accounts associated with a User, you would do the following in the UserController:

class UserController extends Controller {

    // Assuming $user has been created
    $accounts = $user->find(1)->accounts(); // returns an Array of Accounts of all the accounts associated to user 1

    // You could also use the pivot property to access all the pivots associated with User
    $accounts = $user->find(1)->pivot->accounts->all; // returns an Array of Accounts of all the accounts associated to user 1

}
Clone this wiki locally