Now, let's allow users to reply to tweets. Add the line below in start/routes.js:

// start/routes.js'/tweets/reply/:id', 'TweetController.reply').middleware(['auth:jwt']);

This route takes the ID of the tweet a user wants to reply to.

Next, let's create the reply method. Add the code below in TweetController:

Table of Contents

    // app/Controllers/Http/TweetController.js
    // add this at the top of the file
    const Reply = use('App/Models/Reply')
    async reply ({ request, auth, params, response }) {
        // get currently authenticated user
        const user = auth.current.user
        // get tweet with the specified ID
        const tweet = await Tweet.find(
        // persist to database
        const reply = await Reply.create({
            reply: request.input('reply')
        // fetch user that made the reply
        await reply.load('user')
        return response.json({
            status: 'success',
            message: 'Reply posted!',
            data: reply

    Then we fetch the tweet with the specified ID. Using the authenticated user's ID, tweet ID and the reply message entered by the user, we create and persist the reply in the database. We eager load the user that made the reply and finally return a JSON object containing a message and the newly created reply.

    Chimezie Enyinnaya

    16 posts

    Software Developer [PHP Laravel JavaScript NodeJS AdonisJS VueJS] | movie lover | run