Save vs update in laravel

These methods both allow you to save data to a database.

The save() method performs an INSERT when you create a new model which is currently is not present in your database table:

 $flight = new Flight;

 $flight->name = $request->name;

 $flight->save(); // it will INSERT a new record

Also it can act like an UPDATE, when your model already exists in the database. So you can get the model, modify some properties and then save() it, actually performing db's UDPATE:

$flight = App\Flight::find(1);

$flight->name = 'New Flight Name';

$flight->save(); //this will UPDATE the record with id=1

Theupdate() method allows you to update your models in more convenient way:

App\Flight::where('active', 1)
          ->where('destination', 'San Diego')
          ->update(['delayed' => 1]); // this will also update the record

So you don't even need to assign the retrieved model to any variable. Updated properties are passed as arguments.

Examples and more info in the Laravel's docs.


There is only one thing left unsaid in what @ginopane told about the difference and it's that if you use update method on a query builder result then laravel will ignore $fillable or $guard array of your model. This is especially important if you want to use Input::all() as an argument to update:

Post::where('id', $id)->update(Input::all());

So in this case if you use App\Flight::where('active', 1)->update(Input::all()); everything in your database will be updated even if you put it in $fillable. So make sure to use save and update methods on Eloquent instance and not Query builder one. The following code will be fine even if the user submit fields that you don't want to insert or update in your databse table:

// User model
protected $fillable = ['email', 'name'];


// controller
public function update($id)
{
    $user = User::findOrFail($id);

    // validate the input here, use Request to do the job or whatever you like

    $user->update(Input::all());

    return view('some_view')->with('notice', 'user updated');
}

Now, no matter what with the FORM being passed here, only name and email will be updated.

Hope this complete @ginopane answer


save() : you can look to it as the equivalent of the INSERT in sql, it will create a new model (and insert it in the database)

To create a new record in the database, create a new model instance, set attributes on the model, then call the save method

update() : you can look to it as the equivalent of the UPDATE in sql, it will create a new model (and insert it in the database)

The save method may also be used to update models that already exist in the database. To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method. Again, the updated_at timestamp will automatically be updated, so there is no need to manually set its value

code

    $flight = App\Flight::find(1);
    if (empty($flight)) {// you can do this condition to check if is empty
        $flight= new Flight;//then create new object
    }

    $flight->name = 'New Flight Name';

    $flight->save(); //this will UPDATE the record with id=1

for more detail doc

Tags:

Laravel 5