Build a Twitter Clone With Adonis and Vue

User and Tweets Relationship

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:

// app/Models/Tweet.js

user () {
    return this.belongsTo('App/Models/User')

Like this article? Follow @ammezie on Twitter