What's difference between Laravel Model functions “create” and “insert”?

后端 未结 3 1726
春和景丽
春和景丽 2021-01-14 20:50

Laravel Model allows two functions for inserting the values to the database table. They are

Create: User::create([\'id\'=>1,\'name\'=>\'stack\']);

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 21:30

    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.

提交回复
热议问题