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
https://adonisjs.com/docs/4.0/lucid#_inserts_updates

Update is mostly ment for bulk updates, not per ID https://adonisjs.com/docs/4.0/lucid#_bulk_updates

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

2 Likes

Hi
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':params.id,'user_id':auth.current.user.id})
        folder.folder_name = foldername_info
        await folder.save()
        //folder.update
        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' })
                               .returning('*')

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:

3 Likes

Thanks @willvincent I have updated :slightly_smiling_face: