How can I get data result after updated?

#1

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

0 Likes

#2

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

#3

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

#4

and save returns saved model

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

1 Like

#6

Hi @Melchyore

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

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

1 Like