Home > PHP Framework > Laravel > body text

How to use laravel create method

PHPz
Release: 2023-04-12 09:51:25
Original
1561 people have browsed it

In Laravel, we can use the create method to quickly create a new record and save it to the database. This article will introduce how to use Laravel's create method.

The create method allows us to quickly create and save a new Eloquent model without using the save method. We can pass an array as a parameter to the create method, which contains the attribute values ​​​​of the new model. The create method automatically fills the model properties using the fill method before saving the model.

When the create method is called, Laravel will automatically perform the following operations:

  1. Create a new Eloquent model instance;
  2. Use the fill method to fill the attributes of the model ;
  3. Use the save method to save the model to the database;
  4. Return a created model instance.

The following is an example of using the create method to create a new model:

$user = App\User::create([
   'name' => 'John Doe',
   'email' => 'john@example.com',
   'password' => Hash::make('password'),
]);
Copy after login

In this example, we create a new User model and store its attribute values ​​​​in a in the array. When the create method is executed, this array is passed to the create method and the properties of the User model are filled using the fill method. Finally, Laravel will automatically save the User model to the database and return a created User model instance.

In addition to the methods used in the above examples, the create method can also accept other parameters. For example, if we want to create multiple records through create method, we can use createMany method:

$users = App\User::createMany([
   [
       'name' => 'John Doe',
       'email' => 'john@example.com',
       'password' => Hash::make('password'),
   ],
   [
       'name' => 'Jane Doe',
       'email' => 'jane@example.com',
       'password' => Hash::make('password'),
   ],
]);
Copy after login

createMany method accepts an array containing multiple arrays. In this example, we create two new User models and store their property values ​​in an array containing two arrays.

One thing to note when using the create method is that we need to specify which attributes can be mass assigned in the $fillable attribute of the model. If we do not use the create method, but manually create the model and assign values ​​to its properties, we need to manually save the model using the save method before saving the model.

In short, Laravel's create method provides us with a way to quickly create a new model and save it to the database. We just need to store the model's properties in an array and pass this array to the create method. If you need to create multiple records, you can use the createMany method.

The above is the detailed content of How to use laravel create method. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template