Geek Culture
Published in

Geek Culture

Laravel Tutorial ~ Laravel Eloquent Relationships

Hello, how are you all friends, I hope you are always healthy and successful.

This time we will discuss Eloquent between table relations. This feature will make it easier for you to make relationships more concise and simple. If we use this SQL command, it will make the relation between tables longer. Let’s discuss Relationship on Eloquent.

Before I continue, there may be some tutorials that my friends are interested in.

Using Scope in Laravel

Complete Tutorial Using Soft Delete in Laravel

How to Use Seeder in Laravel

Laravel Database Relationship

  1. A one-to-one relationship where a data in one table only has a relationship to a data in another table. For example, a data table tb_User has a relationship 1 phone number in the tb_Contact table.
  2. One-to-many relationship where one data in one table has a relationship to some data in another table. For example, a data table tb_Category has a relationship with many data items in tb_Inventory. Or in other words, 1 category has a lot of inventory data.
  3. Many to one relation (One to many Inverse) which is the opposite of one to many relation. For example, we want to know what category the item data in tb_Inventory belongs to, then this relation will be used.
  4. Many-to-many relationships where a lot of data in a table has a relationship to many data also in other tables. The relationship is formed through an auxiliary table. For example, many records in the tb_Student table have a borrowing relationship to many data in the tb_Buku table. The relationship is formed with a help table named tb_Transaksi.

One-To-One Eloquent relationship

This is the most basic relationship. For example: Each User has 1 Phone. The first argument passed to the hasOne method is the associated model name.

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

The hasOne function now expects a user id field in the Profile model. If your column names are different, add a second argument to the hasOne relationship with the other column names:

return $this->hasOne(Profile::class, 'foreign_key');
  • Foreign Key
    Eloquent determines the foreign key based on the model name. In this case, the Profile model is assumed to have a foreign_key default author_id.
    Specifies a Foreign Key where the default User and author relationship is user_id. To set the Foreign Key manually, it can be written as the second parameter.
return $this->hasOne(Profile::class, 'foreign_key','local_key');
  • Inverse Relationships Foreign Key.
    In the previous example, Eloquent tried to match the user_id of the Phone model to the id of the User model. The default foreign key name is the method name of the relationship plus the _id suffix, so in the example it is user_id.
    If it doesn’t match, it can be written in the second argument of belongsTo:
public function profile():
{
return $this->belongsTo(Profile::class);
}
  • Inverse Relationships Primary Key
    Apabila diiginkan relasi tidak menggunakan default primary key, dapat dituliskan di parameter ketiga belongsTo:
return $this->belongsTo(Profile::class, 'foreign_key','local_key');

Eloquent One-To-Many relationship

Another thing that is very important, perhaps even the most important, is the One-To-Many relationship. Also known as hasMany-relationship, this relationship defines the relation that ‘one item has many other items. This relationship is very similar to the one above.

To continue our blogging example, say that a profile has multiple posts. Go to your Profile model and add the following method:

public function posts(): HasMany
{
return $this->hasMany(Post::class);
//Or: return $this->hasMany(Post::class, 'foreign_key');
}
  • Foreign dan Local Key
    The use of foreign and primary (local) keys in One to Many is also similar to One to One.
return $this->hasMany(Post::class, 'foreign_key');
return $this->hasMany(Post::class, 'foreign_key','local_key');

Many to Many

This relation is also widely used. Its use is slightly more complex than the previous 2 types of relations.
The following example is where there are users, roles, and role_user. A user can have many roles and a role can have many users.
role_user is the default intermediate or pivot table. The table name is from both the role and user table in alphabetical order
Relationships are defined using the belongsToMany method.

public function profiles(): BelongsToMany
{
return $this->belongsToMany(Profile::class);
}
  • Pivot Table
    As mentioned earlier pivot table names are generated from both table names and are sorted alphabetically. If needed, it can be defined in the second parameter of the belongsToMany method.
return $this->belongsToMany(Profile::class,'role_user');

In addition to defining the pivot table name, as in other types of relationships, the key for each table can be defined as in the following example:

return $this->belongsToMany(Profile::class,'role_user','user_id','role_id');

If there are other fields/columns in the pivot table, and defined by writing to the withPivot method.

return $this->belongsToMany(Profile::class)->withPivot('column1','column2');

That’s the tutorial this time that I can convey, hopefully it will be useful.

Thank you…….

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store