How does the Laravel Model Save method work?

Asked

Viewed 1,100 times

0

Saving:

var $newOs    = new OS;
$newOs->date  = date;
$newOs->name  = name;
$newOs->Area  = area;
$newOs->save();

Note that on the last line is the $newOS->save, in which no parameters are passed. But in the documentation of the Illuminate Eloquent Model, it has the method save() receiving parameters. I suspect it’s something in constructor, but I found nothing.

How this works by lowering the cloths if no parameter is passed in the function?

I couldn’t reproduce this with Object Orientation.

Either I didn’t know how to do it or it’s something Laravel’s maker did internally to make it happen.

  • 1

    The Illuminate\Eloquent\Model uses the Trait GuardsAttributes, that one trait treats the attributes that were inserted directly into the object, as in your example. then it fetches all attributes and inserts into the method save(). Right on top of that.

  • I think it’ll help: Documentation on Laravel Index

2 answers

1

If you look at the source code of the Laravel on Github Model.php:

This save method parameter is an array of options that serves for you to disable the date and time record for this specific query

You can go through something like this:

var $newOs    = new OS;
$newOs->date  = date;
$newOs->name  = name;
$newOs->Area  = area;
$newOs->save([
  'timestamps' => false, // Desative o registro de data e hora na inserção e atualização
  'touch'      => false, // Desativa o modelo de relacionamento que toca na classe atual durante o escopo de retorno de chamada especificado.
]);

It’s quite common, looking at the Laravel source code class on Github will help you better understand how it works.

  • Although it wasn’t my question, I thought it was nice to know that. Thank you.

0

It is the MYSQL Commit, you do all processes and then commit to execute the save query does just that, you can use it for Iserir and Edit data, save is the final word.

Example of inserting with save:

public function insert(Request $request, Model $model){
  $model->nome = $request->nome;
  $model->column = $request->column;
  $model->save();
}

Example of update with save:

public function update(Request $request, $id){
    $model = Model::findOrFail($id);

    $model->nome = $request->nome;
    $model->column = $request->column;
    $model->save();

}

Browser other questions tagged

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