What is the difference between "merge" and "Fill" in Adonisjs' Lucid?

Asked

Viewed 135 times

1

I have several years of experience with Laravel and am taking a look at the Adonisjs framework. It is very similar to the Laravel in several points, but specifically I’m having a doubt regarding the filling of data of a model Lucid.

For example, in Laravel I usually use the method fill to fill only some desired fields through a array, to save that information.

I tried to use the fill in Adonisjs in a similar way, but an error was appearing, as if the other fields had been deleted. When I used the merge, I didn’t have that problem, the behavior was similar to that of the fill of Laravel.

For example:

const usuario = await Usuario.find(1)

usuario.merge({"nome": "Wallace"});

usuario.save(); // Aqui é ok

usuario.fill({"nome" : "Wallace"});

usuario.save(); // Aqui dá erro

On that note, I ask:

  • What are the differences between fill and merge in Adonisjs?
  • When using one or the other?

1 answer

1


No Lucid when you use the method fill, all other existing values will be removed keeping only what you have just set. You can use this method when you want to insert a record with fully defined values in the code itself, for example.

The method merge modifies only the element you specified, keeping all others. You can use this method when you want to modify/add only some of the attributes that were sent.

You can check the official documentation of Adonis on the two methods.

  • You don’t need to delete your answers and post a new one. Just edit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.