DEV Community

Carlos Daniel Ortega Hernandez
Carlos Daniel Ortega Hernandez

Posted on

Eloquent ORM relationship (part 1)

Relational database

A relational database is one that allows us to divide a record into several tables.

One-to-one relationship

A one-to-one relationship in a database is a relationship between two tables where each record in one table is related to exactly one record in the other table.

note: With the command 'php artisan make:model Profile -mf' the model and the factory are created and the migration is created. The first letter must be capitalized and the word singular.

An example of this is relating a user to a profile, a user has a profile and a profile belongs to a user.

For this we use the hasOne method.

public function profile()
{
    return $this->hasOne(Profile::class);
}
Enter fullscreen mode Exit fullscreen mode

One-to-many relationship

A one-to-many relationship in a database refers to a relationship between two entities, in which an instance of one entity is associated with zero, one, or many instances of the other entity

For this we use the hasMany method.

public function users()
{
    return $this->hasMany(User::class);
}
Enter fullscreen mode Exit fullscreen mode

And the method we use at the model level is belongsTo.

public function level()
{
    return $this->belongsTo(Level::class);
}
Enter fullscreen mode Exit fullscreen mode

This way we will be able to see to which level the user belongs.

Many-to-many relationship

A many-to-many relationship in a database refers to a relationship between two entities, in which an instance of one entity can be associated with zero, one, or many instances of the other entity, and vice versa.

To make a many-to-many relationship we need an intermediate table in which we have the foreign keys of each table, as in the following example in a relationship of the tables groups and users.

And the method we use is the belongsToMany

public function groups()
{
    return $this->belongsToMany(Group::class);
}
Enter fullscreen mode Exit fullscreen mode
public function users()
{
    return $this->belongsToMany(Users::class);
}
Enter fullscreen mode Exit fullscreen mode

To configure the date of creation and update, that appear in null, for it the following method is placed. Which what it does is to put the dates automatically, you can also remove the $table->timestamps(); so that it does not have those columns.

public function users()
{
    return $this->belongsToMany(Users::class)->withTimestamps();
}
Enter fullscreen mode Exit fullscreen mode

One-to-one relationship through...

A one-to-one relationship through another table refers to a relationship between two tables in a database in which each row in one table has a relationship with a row in the other table, and this relationship is established through an intermediate table.

To do this we use the hasOneThrough method, that way we can query a field of the table without calling the one in between.

public function location()
{
    return $this->hasOneThrough(Location::class, Profile::class);
}
Enter fullscreen mode Exit fullscreen mode

That would translate as user has location through profile.

The profile model must have the hasOne method, for the method to work.

public function location()
{
    return $this->hasOne(Location::class);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)