Also, let's add ability for users to unfollow one another. Add the code below to start/routes.js within the users group routes we defined in the previous lesson:

// start/routes.js

// unfollow user
Route.delete('/unfollow/:id', 'UserController.unFollow')

This also takes the ID of the user we want to unfollow as a parameter.

Next, add the code below to UserController:

Table of Contents

    // app/Controllers/Http/UserController.js
    async unFollow ({ params, auth, response }) {
        // get currently authenticated user
        const user = auth.current.user
        // remove from user's followers
        await user.following().detach(
        return response.json({
            status: 'success',
            data: null

    The is simply an inverse of the follow method as it makes use of the detach method to remove the user with the specified ID from the list of followers of the authenticated user.

    Chimezie Enyinnaya

    20 posts

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