How can I get data result after updated?

I used Lucid like this.

const customer await Customer.query().where('id', id).update({username: 'admin'})

and then return customer the value I got is 1 but I want the result of data

The default behavior of your operation is to return the number of affected rows.
In your case, one row was affected, so you got 1.

You can get the related data by, for example, querying that row given its ID:

const customer = await Customer.find(id) // I suppose you have got the ID  from somewhere
1 Like

Hi @9wisit

As addition to begueradj I’d like to add, that in your current case you can also use merge() with save(). Merge merges new data to old model and save returns saved model

Scroll down a little from there

Update is mostly ment for bulk updates, not per ID

1 Like

and save returns saved model

@ McSneaky you are wrong.
According to this the save method returns a Boolean.

1 Like

Hi @Melchyore

You’r right, thnx for pointing that out :slight_smile:

It’s create() not save() that returns model


const customer await Customer.query().where('id', id).update({username: 'admin'})

The above code returns Boolean 1 or 0 (true or false).

If you need response data, use the code below

let folder = await Folder.findBy({'id','user_id'})
        folder.folder_name = foldername_info
        return response.status(201).json({
            'Message:':'Successfully updated',
            'Folder': folder

One of the many benefits of using postgres is the returning() method that can be chained onto inserts and updates.

So one could do something like this:

const customer = await Customer.query()
                               .where('id', id)
                               .update({ username: 'admin' })

This makes more sense if you’re doing a bulk insert and want all of the records back though. For a single record I’d do like @RK-dev mentioned above… not forgetting the await on the save of course :slight_smile:


Thanks @willvincent I have updated :slightly_smiling_face: