Yado's avatar
Level 1

What's the difference between save() and create() ?

When should be which one used?

I understand that create can be called statically like MyModel::create()
while save has to be accessed like
$model = new MyModel(); $model->save()
But when should be which function used?

0 likes
6 replies
joedawson's avatar

You can use the save() method to update a model also.

$user = User::first();
$user->email = 'updated@domain.com';
$user->save();
2 likes
tangoG's avatar

Save can be used to both create a new Record and update a existing record . Whereas create is used to create a new record by providing all required field at one time .

But to use create you should define fillable fields in your Model file like this

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['name'];
6 likes
RobinMalfait's avatar
Level 7

Model::create is a simple wrapper around this, if you look at its implementation:

/**
 * Save a new model and return the instance.
 *
 * @param  array  $attributes
 * @return static
 */
public static function create(array $attributes = [])
{
    $model = new static($attributes);

    $model->save();

    return $model;
}
23 likes
bimalshah72's avatar

@Yado

in a simple language,

When you retrieve a record via Eloquent, you can access its properties as follows:

$country = Country::find(1);
print $country->name;

We can update attribute values in the same manner:

$country->name = 'INDIA';

This will set the value in the model instance, but we need to persist the change to the database. We do this by calling the save method afterwards:

$country->save();

This also could be use for creating new record.

If you have a table with lots of columns, then it will become tiresome to assign each property manually like this. Eloquent allows you to fill models by passing an associative array with values, and the keys representing the column names. You can fill a model while either creating or updating it:

$data = [
'name' => 'INDIA',
'area' => '12345',
'language' => 'hindi,
];
Country::create($data);

However, this will throw a MassAssignmentException error.

If the $data array in the previous example came from say, a user's form submission, then they can update any and all values in the same database. Consider that you have a users table with a column called is_admin, which determines whether or not that user can view your website's administration area. Also consider that users on the public side of your website can update their profile. If, during form submission, the user also included a field with the name of is_admin and a value of 1, that would update the column value in the database table and grant them access to your super secret admin area—this is a huge security concern and is exactly what mass-assignment protection prevents. To mark columns whose values are safe to set via mass-assignment (such as name, birth_date, and so on.), we need to update our Eloquent models by providing a new property called $fillable. This is simply an array containing the names of the attributes that are safe to set via mass assignment:

class Country extends Model {

protected $fillable = [
'name',
'area',
'language',
];
}

In short save() method is used both for saving new model, and updating existing one. here you are creating new model or find existing one, setting its properties one by one and finally saves in database, while in create method you are passing array, setting properties in model and persists in database in one shot.

9 likes
phildawson's avatar

@Dixens thats adding slightly more confusion as thats in the context of a Relation not Model.

With the context of Model which everyone is talking about save accepts an array of options eg

$model->timestamps = false;
$model->save();

=

$model->save(['timestamps' => false]);
1 like

Please or to participate in this conversation.