In the previous lesson, we gave users ability to post tweets. Now, let's fetch a single tweet. Add the line below in start/routes.js:

// start/routes.js

Route.get('/tweets/:id', '')

Next, add the code below in TweetController.js:

// app/Controllers/Http/TweetController.js

async show ({ params, response }) {
    try {
        const tweet = await Tweet.query()

        return response.json({
            status: 'success',
            data: tweet
    } catch (error) {
        return response.status(404).json({
            status: 'error',
            message: 'Tweet not found'

We fetch the tweet with the specified ID. In addition to the fetching the tweet, we also eager load the user that created the tweet, the favorites of the tweet, the replies of the tweet, and user that made the replies. Then we return the tweet. If there is no tweet with the specified ID, we simply return an appropriate message.

Table of Contents

    Chimezie Enyinnaya

    20 posts

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