Skip to main content
LaravelUncategorized

Explained types and how to use relations in Laravel with Example

In Laravel, there are four types of relationships that you can use to define the way that different database tables are related to each other.

Here’s an explanation of the different types of relationships in Laravel, along with examples of how to use them:

  1. One-to-One Relationships: This type of relationship is used when a single record in one table is related to a single record in another table. For example, if you have a User model and a Profile model, where each user has a single profile, you could define a one-to-one relationship like this:
class User extends Model
{
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

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

With this relationship defined, you can easily retrieve a user’s profile like this:

$user = User::find(1);
$profile = $user->profile;
  1. One-to-Many Relationships: This type of relationship is used when a single record in one table is related to multiple records in another table. For example, if you have a User model and a Post model, where each user has many posts, you could define a one-to-many relationship like this:
<?php 
class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

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

With this relationship defined, you can easily retrieve a user’s posts like this:

<?php 
$user = User::find(1);
$posts = $user->posts;
  1. Many-to-Many Relationships: This type of relationship is used when multiple records in one table are related to multiple records in another table. For example, if you have a User model and a Role model, where each user can have many roles and each role can be assigned to many users, you could define a many-to-many relationship like this:
class User extends Model
{
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

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

With this relationship defined, you can easily retrieve a user’s roles like this:

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

You can also add or remove roles for a user like this:

$user->roles()->attach($role_id); // Add a role
$user->roles()->detach($role_id); // Remove a role
  1. Polymorphic Relationships: This type of relationship is used when a single table can be related to multiple other tables. For example, if you have a Comment model that can be associated with either a Post or a Video model, you could define a polymorphic relationship like this:
class Comment extends Model
{
    public function commentable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}

class Video extends Model
{
    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}

With this relationship defined, you can easily retrieve the comments for a post or a video like this:

$post = Post::find(1);
$comments = $post->comments;

$video = Video::find(1);
$comments = $video->comments;
``

By using relationships in your Laravel application, you can easily work with related data and build more complex features and functionality.

For more information on Laravel Octane, check out its official documentation