Create Method In Laravel
In addition to the save
and saveMany
methods, you may also use the create
method, which accepts an array of attributes, creates a model, and inserts it into the database. Again, the difference between save
and create
is that save
accepts a full Eloquent model instance while create
accepts a plain PHP array
:
$post = App\Post::find(1);
$comment = $post->comments()->create([
'message' => 'A new comment.',
]);
Before using the create
method, be sure to review the documentation on attribute mass assignment.
You may use the createMany
method to create multiple related models:
$post = App\Post::find(1);
$post->comments()->createMany([
[
'message' => 'A new comment.',
],
[
'message' => 'Another new comment.',
],
]);