What Is Create Method In Laravel Framework With Example

admin_img Posted By Bajarangi soft , Posted On 10-09-2020

Eloquent relationships are defined as methods on your Eloquent model classes. Since, like Eloquent models themselves, relationships also serve as powerful query builders, defining relationships as methods provides powerful method chaining and querying capabilities.Today we are going to discuss how can we use create method in laravel and for what we use create method in laravel with example

What Is Create Method In Laravel Framework With Example

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.',
    ],
]);

 

Related Post