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

后端 未结 3 1728
春和景丽
春和景丽 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:16

    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'=>'test@gmail.com']);
    

    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'=>'test@gmail.com'])
    

提交回复
热议问题