Having defined the models, now let's define their relationships. We'll start with User and Tweet models. A user can post as many tweets as he/she wants, but a tweet can only belong to a user. In other words, the relationship between User and Tweet is a one-to-many relationship.

To replicate this in code, open app/Models/User.js and add the code below to it:

// app/Models/User.js

tweets () {
    return this.hasMany('App/Models/Tweet')
}

To complete the relationship, we need to define the inverse relationship on the Tweet model. Open app/Models/Tweet.js and add the code below to it:

Table of Contents

    // app/Models/Tweet.js
    
    user () {
        return this.belongsTo('App/Models/User')
    }

    Chimezie Enyinnaya

    19 posts

    Software Developer [PHP Laravel JavaScript NodeJS AdonisJS VueJS] | movie lover | run http://openlaravel.com