Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference between save, fill, create in laravel eloquent

So i am new to laravel,
I don’t know the difference between save, fill and create in laravel eloquent.
can anybody describe it?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

save() method is when you already assign a value to a model instance. for example

$user = new User;
$user->name = "anything";
$user->save();
dd($user); // {'id': 1, 'name': 'anything'}

save() can also for update a value

$user = User::first(); // { 'id': 1, 'name': "anything" }
$user->name = "change";
$user->save();
dd($user); // {id: 1, name: "change"}

create() is a static function, need array parameter to create a new record

$user = User::create(['name' => 'new user']);
dd($user); // {id: 2, name: 'new user'}

fill() is same like save() method but without create a new record. need to create a new instance before can use fill()

$user = new User;
$user->fill(['name' => 'last user']);
echo count(User::all()); // We count users on DB and result is 2
$user->save(); // This will save 'last user' to DB
echo count(User::all()); // result is 3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading