loading...
Cover image for Laravel Tutorial #4: Database Relations

Laravel Tutorial #4: Database Relations

ericnanhu profile image Eric Hu Originally published at techjblog.com ・3 min read

This section might be confusing for beginners. I recommend reading the official documentation before you start.

Introduction

Here, I’d like to introduce some basic relationships. I only picked the ones we need to use to build a simple blogging system, since some relationships might be too hard for beginners.

One to One

This is the most basic relation. For example, each User is associated with one Phone. To define this relationship, we need to place a phone method on the User model.

class User extends Model
{
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}

One to One (Inverse)

The inverse of “has one” would be “belongs to one”. For example, each Phone would belong to one User. In order to define the inverse of the one to one relationship. We place a user method on the Phone model.

class Phone extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

One to Many

A one-to-many relationship is used to define relationships where a single model owns any amount of other models. For example, one Category could have many Posts. Just like one-to-one relation, it can be defined by putting a posts method in the Category model.

class Category extends Model
{
    public function posts()
    {
        return $this->hasMany('App\Post');
    }
}

One to Many (Inverse)

However, sometimes we need to find the category through the post. The inverse of “has many” would be “belongs to”. In order to define the inverse of the one-to-many relationship. We place a category method on the Post model.

class Post extends Model
{
    public function category()
    {
        return $this->belongsTo('App\Category');
    }
}

Many to Many

Many-to-many relations are slightly more complicated than hasOne and hasMany relationships. An example of such a relationship is a post with many tags, where the tags are also shared by other posts. We’ll talk about this later.

Design Relationships

There are six relationships we need to take care of.

  1. Each user has multiple posts
  2. Each category has many posts
  3. Each tag belongs to many posts
  4. Each post belongs to one user
  5. Each post belongs to one category
  6. Each post belongs to many tags

Define Relationships

Now, we can define relationships for our database tables.

Between User and Post (One to Many)

If you followed the previous section, we already added a “user_id” column in the “posts” table to store which user does the post belongs to.

database/migrations/create_posts_table.php:

$table->bigInteger('user_id');

This column will store the id of the user that this post belongs to. Now we can define the relationships in the models.

app/User.php:

/**
 * Get the posts for the user.
 */
public function posts()
{
    return $this->hasMany('App\Post');
}

app/Post.php:

/**
 * Get the user that owns the post.
 */
public function user()
{
    return $this->belongsTo('App\User');
}

Between Category and Post (One to Many)

Again, we need to have a “category_id” column in the “posts” table.

database/migrations/create_posts_table.php:

$table->bigInteger('category_id');

Define the relationships in the models.

app/Category.php:

/**
 * Get the posts for the user.
 */
public function posts()
{
    return $this->hasMany('App\Post');
}

app/Post.php:

/**
 * Get the category that owns the post.
 */
public function category()
{
    return $this->belongsTo('App\Category');
}

Between Tag and Post (Many to Many)

This one is a bit more complicated, it requires a Many To Many Relationship and an extra database table “post_tag“. This table is called a pivot table.

First, create a new migration file:

php artisan make:migration create_post_tag_table

Edit create_post_tag_table.php,

Schema::create('post_tag', function (Blueprint $table) {
    $table->bigInteger('tag_id');
    $table->bigInteger('post_id');
});

Now we can define the relationships in the models.

app/Tag.php:

public function posts()
{
    return $this->belongsToMany('App\Post');
}

app/Post.php:

public function tags()
{
    return $this->belongsToMany('App\Tag');
}

Here Laravel assumes there is a “post_tag” table and there are two columns “post_id” and “tag_id” in it. The name of the table needs to be in alphabetical order. If you named them differently, “tag_post”, for example, you need to specify them in the model.

public function tags()
{
    return $this->belongsToMany('App\Tag', 'tag_post');
}

Next Post: Laravel Tutorial #5: Admin Panel (Voyager)

Related Articles

How to Make Your Server More Secure

Laravel Tutorial For Beginners

Django Tutorial For Beginners

Discussion

pic
Editor guide