Difference between the create and Fill method - Standard

Asked

Viewed 5,888 times

3

I was developing a web service with Laravel, and I realized that I can save the data in my database in two ways:

Product::create($request->all());

or else:

$product = new Product();
$product->fill($request->all());
$product->save();

My question is whether there is any difference between these two forms. And when I should use one or the other.

3 answers

8

The two methods do the same things, with only one difference:

If you already have a model instance, you may use the Fill method to populate it with an array of Attributes:

$flight->fill(['name' => 'Flight 22']);

In other words, you use the fill if you have already instantiated the model, if you do not continue using the create.

Update

Wrong. The two don’t do the same thing. Fill just fills the attributes in the model. create creates the data. Basically create uses Fill internally and then calls save. If you use Fill without save, records in the database are not affected.

Source: Mass Assignment

  • 3

    -1 Wrong. Both don’t do the same thing. The fill only fills the attributes in the model. O create creates the data. Basically, the create uses the fill internally and then calls save. If you use the fill without save, records in the database are not affected.

  • Thank you, @Wallacemaxters

6

The method fill means "fill in".

It just fills in your Model information.

The Fill used thus:

$usuario = Usuario::first();

$usuario->fill(['nivel_id' => 1, 'nome' => 'Wallace']);

It’s the same as doing it this way:

$usuario = Usuario::first();

$usuario->nivel_id = 1;
$usuario->nome = 'Wallace';

If you just call fill, nothing will occur in the database, but only in the instance of your Model.

You need to call the method save for the information to be synchronized in the database.

The create serves to create a record. The create always creates, unlike save. The save creates if it does not exist and, if it exists, updates.

2

The first example of code, creates an instance of the class and takes the data passed and writes

//instancia uma classe Produto
//método atribui os valores configurados no fillable
//grava um novo registro na sua tabela e desenvolve uma instância desse registro
Product::create($request->all()); 

the other way serves to assign new values to non-existent or even existing data, ie, the second code can be used to save a new record or even change data of a record that already exists, calling finally the method save(), example

$product = new Product();
$product->fill($request->all());
$product->save();

or

// buscando registro de número 1
$product = Produto::find(1) 
// verificando se encontrou o produto
if ($produto)
{
    //atribuindo novos valores ou apenas alguns valores
    $product->fill($request->all());
}
// salvando os dados passados
$product->save();

note, that the method fill only accepts the values set in fillable, but, not required to pass all values and only past values will be changed or entered and also the values passed by this method does not guarantee that the data will be saved, because finally should call the method save().

In the same line as create which already makes at once the creation of a new record, has the method update which has the purpose of assigning the values and immediately after saving the changes, example:

// buscando registro de número 1
$product = Produto::find(1) 
// verificando se encontrou o produto
if ($produto)
{
    //atribuindo novos valores ou apenas alguns valores
    // e salvando as alterações (sem precisa chamar método save())
    $product->update($request->all());
}

References:

  • Ahh, so that means that Fill also serves to update if I have the product instance, if I don’t have instance, it creates. Apparently replaces create and update very well.

  • 1

    the method fill serves to fill a class of values, both in new record mode as well as record editing ... @Marcelotorres only that the data is only really saved from the method ->save(). Already Create and Update it makes the process of creating and/or updating in the same method call

Browser other questions tagged

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