Eloquent: Relationships in Laravel with example

Posted by

Laravel framework, relationships refer to the associations established between database tables/models. Laravel provides a convenient and expressive syntax for defining and managing these relationships, making it easier to work with related data.

Laravel offers several types of relationships:

One-to-One Relationship:

This type of relationship exists when a single record in one table/model is associated with a single record in another table/model. For example, a User model may have a one-to-one relationship with a Profile model, where each user has one profile associated with them.

Consider the scenario where we have a User model and a Profile model. Each user has one profile associated with them.

// In the User model

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

// In the Profile model

class Profile extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

With these relationships defined, you can access the profile of a user like this:

$user = User::find(1);
$profile = $user->profile;

One-to-Many Relationship:

In a one-to-many relationship, a single record in one table/model is associated with multiple records in another table/model. For instance, a User model may have a one-to-many relationship with a Post model, where a user can have multiple posts.

Let’s consider a scenario where we have a User model and a Post model. A user can have multiple posts.

// In the User model

class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

// In the Post model:

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

You can retrieve all posts belonging to a user like this:

$user = User::find(1);
$posts = $user->posts;

Many-to-Many Relationship:

Many-to-many relationships occur when multiple records in one table/model are associated with multiple records in another table/model. For example, a User model may have a many-to-many relationship with a Role model, where users can have multiple roles and roles can be assigned to multiple users.

Let’s consider a scenario where we have a User model and a Role model. Users can have multiple roles, and roles can be assigned to multiple users.

// In the User model: 

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

// In the Role model:

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

You can retrieve all roles associated with a user like this:

$user = User::find(1);
$roles = $user->roles;

These are just a few examples of the relationships you can define in Laravel using eloquent. Laravel provides a rich set of methods and conventions to work with relationships, allowing you to easily navigate and manipulate associated data in your applications.

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
1
0
Would love your thoughts, please comment.x
()
x