alexa
Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

Difference between save, fill, create in laravel eloquent ?

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().


53 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online