Difference between create and fill - laravel method

3

I was developing a web service with laravel, and I realized that I can save the data in my bank 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 should I use one or the other.

    
asked by anonymous 23.04.2018 / 14:08

3 answers

5

Both methods do the same things, with only one difference:

  

If you already have a model instance, you can use the fill method to   populate it with an array of attributes:

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

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

Update

  

Wrong. The two do not do the same thing. The fill only fills the   attributes not model. Create creates the data. Basically, create uses   fill it internally, and then call save. If you use fill without   save, the records in the database are not affected.

Source: Mass Assignment

    
23.04.2018 / 14:24
4

The fill method means "fill in".

It only fills in your Model information.

The used fill:

$usuario = Usuario::first();

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

It's the same as doing so:

$usuario = Usuario::first();

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

If you only call fill , nothing happens in the database, but only in your Model instance.

You need to call the save method so that the information is synchronized in the database.

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

    
24.08.2018 / 19:07
1

The first code example, 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 is to assign new values to nonexistent or existing data, that is, the second code can be used to save a new record or even change data from a record that already exists, finally calling the 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 fill method only accepts the values set in fillable , but it is not necessary to pass all the values and only the values passed are inserted or inserted and also the values passed by this method does not guarantee that the data will be saved, because you should finally call the save() method.

In the same line as the create that already does the creation of a new record at once, it has the update method, which has the purpose of assigning the values and soon after saving the changes, for 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());
}

23.04.2018 / 14:42