What is different between save(), create() function in laravel 5
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Understanding the Difference Between save() and create() Functions in Laravel 5
Body: Laravel is a popular PHP framework that offers numerous features to make web development easier and more efficient. One of these features is the Eloquent ORM, which provides an object-relational mapping layer for working with databases. In this blog post, we will explore the differences between two crucial methods in this ORM: `save()` and `create()`, and when to use them effectively.
1. The save() Method
The `save()` method is responsible for saving an existing model's data to a database table, allowing us to update or insert new records. Here's how it works:
- We first instantiate the required model and fill in its attributes with the necessary data using either mass assignment or Eloquent mutators.
- Next, we call the `save()` method on our object, which will perform any required database actions according to whether we're creating a new record (insert) or updating an existing one (update).
- If a new record is created, it returns the primary key of the inserted row; if it's an update, it returns true.
Example:
```php
// Create a new user with a name and an email address
$user = new User();
$user->name = 'John Doe';
$user->email = 'john@example.com';
$user->save(); // Create a record if not exists or update existing one
```
2. The create() Method
The `create()` method, on the other hand, is designed specifically for creating new records in a database table. It does not allow us to modify any existing data. Here's how it works:
- We instantiate the model and fill its attributes with the necessary data using either mass assignment or Eloquent mutators.
- Then, we call `create()` on our object, which will create a new record in the database table without any checks for existing records. The method returns an instance of the created model.
Example:
```php
// Create a new user with specific attributes
$user = User::create([
'name' => 'Jane Doe',
'email' => 'jane@example.com'
]);
```
3. When to Use Each Method and Best Practices
- `save()` should be used if you need to update an existing model or create a new one when no checks are needed for duplicate records. It can also help avoid multiple queries in case of updating many fields.
- `create()` is best suited for creating new records without worrying about checking for duplicates or updating existing records. It's particularly useful when making RESTful API calls or handling bulk insertions.
When to Use create() and save():
- When you need to handle multiple models and only use save() if an update is needed:
```php
$users = User::all(); // Get all users
foreach ($users as $user) {
if (some condition) // Check for specific conditions
$user->save();
}
```
- When you need to create a new record and don't care about duplicates or existing records:
```php
$newUsers = User::createMany([
[
'name' => 'New User',
'email' => 'newuser@example.com'
]
]); // Create multiple users at once
```
Conclusion: Both `save()` and `create()` are essential methods in Laravel's Eloquent ORM, but they have different purposes. Understanding the differences between them allows you to choose the most appropriate method and optimize your code for better performance and cleaner design.