Reputation: 585
Laravel Model allows two functions for inserting the values to the database table. They are
Create:
User::create(['id'=>1,'name'=>'stack']);
Insert:
User::insert(['id'=>2,'name'=>'overflow']);
I found they perform similar operations. What's difference between them?
Upvotes: 6
Views: 9269
Reputation: 29
In addition to the above, another major difference between the insert and create methods is that the output of the insert method is a boolean value that indicates the success or failure of the insert. But the output of create is an object containing stored data values
Upvotes: 0
Reputation: 11
save()
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.
save() accepts a full Eloquent model instance
create()
while in creating method you are passing an array, setting properties in model and persists in the database in one shot. create() accepts a plain PHP array
Upvotes: 1
Reputation: 31
Insert method : The insert method accepts an array of column names and values.using this method you can insert data without specify fillable and guarded attribute on the model and here created_at and updated_at values put as NULL value by default.
User::insert(['userName'=>'manish','email'=>'[email protected]']);
Create method The create method also used to insert a new model in a single line. It's instance will return you from that method. before using create() will need to specify fillable or guarded attribute on model its protect against mass-assignment by default and its auto fillable value for create_at and updated_at
User::create(['userName'=>'manish','email'=>'[email protected]'])
Upvotes: 3
Reputation: 243
insert() :
If you using insert() method you can't default created_at and updated_at database column it will be null
DefaultUser::insert(['username' => $request->username, 'city' => $request->city, 'profile_image' => $request->profile_image]);
create() : when we use create method you must define this model in fillable fields
Add in Your Model
protected $fillable = ['username','city', 'profile_image'];
Add your Controller
DefaultUser::create(['username' => $request->username, 'city' => $request->city, 'profile_image' => $request->profile_image]);
then we can use create method without **mass assignment error ** basically here , table defined fields are protected in your model
you should define which model attributes you want to make mass assignable. You may do this using the $fillable property on the model
Upvotes: 10
Reputation: 14268
The model does not have an insert
, calling Model::insert
results in a call to (the Query Builder) Builder::insert
through the __call()
magic method which then avoids the Eloquent benefits like setting the timestamps
for created_at
and updated_at
fields.
It also avoids the Mass-assignment protection which Eloquent protects you from inserting unintentional data.
So I would always use create
or setting each field separately (if you need to modify the incoming data) and call save()
on the model instance.
Upvotes: 5