Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Difference between save, fill, create in laravel eloquent ?

By M.K. Verma · 2 May 2022

Difference between save, fill, create in laravel eloquent ?

The are kinda the same, but not quite.

//Creating User with fill()
$user = new User();
$user->fill($validatedUserData);
$user->save();

//Creating User with create();
$user = User::create($validatedUserData);

As you can see, create can do all of 3 lines(with fill function) with just one line. That's essentially a quality of life feature.

Both of this shown above does the same thing.

With that being said, you'd probably want to use create() when making a new entry. But for updating an item, it's better to just do something like this:

public function update(User $user, Request $request){
    $validatedUserData = $request->validate([
        //Validation logic here
    ]);

    $user->fill($validatedUserData);
    $user->save();


}

Note: You need to mark fields as  to use those fields with create() or fill().