What is different between save(), create() function in laravel 5

前端 未结 2 1027
余生分开走
余生分开走 2020-12-13 09:38

I need to know what is the difference of save() and create() function in laravel 5. Where we can use save() and create()

2条回答
  •  借酒劲吻你
    2020-12-13 10:18

    If you are looking for short answer it is on laravel doc as

    The Create Method

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

    if you are looking for details see @Tony Vincent's answer.

提交回复
热议问题