laravel 5 中的 save()、create() 函数有什么不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38843202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:29:33  来源:igfitidea点击:

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

laravellaravel-5laravel-5.2

提问by Nikunj K.

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

我需要知道laravel 5中save()create()功能的区别是什么。我们可以在哪里使用save()create()

回答by Tony Vincent

Model::createis a simple wrapper around $model = new MyModel(); $model->save()See the implementation

Model::create是一个简单的包装器$model = new MyModel(); $model->save()查看实现

/**
 * Save a new model and return the instance.
 *
 * @param  array  $attributes
 * @return static
 */
public static function create(array $attributes = [])
{
    $model = new static($attributes);

    $model->save();

    return $model;
}

save()

节省()

  • save() method is used both for saving new model, and updating existing one. here you are creating new model or find existing one, setting its properties one by one and finally saves in database.

  • save() accepts a full Eloquent model instance

    $comment = new App\Comment(['message' => 'A new comment.']);
    
    $post = App\Post::find(1);
    
    $post->comments()->save($comment);
    
  • save() 方法用于保存新模型和更新现有模型。在这里,您正在创建新模型或查找现有模型,一一设置其属性,最后保存在数据库中。

  • save() 接受一个完整的 Eloquent 模型实例

    $comment = new App\Comment(['message' => 'A new comment.']);
    
    $post = App\Post::find(1);
    
    $post->comments()->save($comment);
    


create()


创建()

  • while in creating method you are passing an array, setting properties in model and persists in the database in one shot.
  • create() accepts a plain PHP array

    $post = App\Post::find(1);
    
    $comment = $post->comments()->create([
        'message' => 'A new comment.',
    ]);
    

    EDIT
    As @PawelMysior pointed out, before using the create method, be sure to mark columns whose values are safe to set via mass-assignment (such as name, birth_date, and so on.), we need to update our Eloquent models by providing a new property called $fillable. This is simply an array containing the names of the attributes that are safe to set via mass assignment:

  • 在创建方法时,您正在传递一个数组,在模型中设置属性并一次性保存在数据库中。
  • create() 接受一个普通的 PHP 数组

    $post = App\Post::find(1);
    
    $comment = $post->comments()->create([
        'message' => 'A new comment.',
    ]);
    

    编辑
    正如@PawelMysior 指出的那样,在使用 create 方法之前,一定要标记其值可以通过批量赋值安全设置的列(例如名称、出生日期等),我们需要通过提供更新我们的 Eloquent 模型一个名为 $fillable 的新属性。这只是一个包含可以通过批量赋值安全设置的属性名称的数组:

example:-

例子:-

class Country extends Model {

    protected $fillable = [
        'name',
        'area',
        'language',
        ];
}

回答by Haritsinh Gohil

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

如果你正在寻找简短的答案,它在 laravel doc 上

The Create Method

创建方法

In addition to the saveand saveManymethods, you may also use the createmethod, which accepts an array of attributes, creates a model, and inserts it into the database.

除了savesaveMany方法之外,您还可以使用create方法,该方法接受属性数组、创建模型并将其插入到数据库中。

Again, the differencebetween saveand createis that

同样,差分之间savecreate

saveaccepts a full Eloquent model instance

save接受一个完整的 Eloquent 模型实例

while createaccepts a plain PHP array:

whilecreate接受一个普通的 PHP 数组

$post = App\Post::find(1);

$comment = $post->comments()->create([
    'message' => 'A new comment.',
]);

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

如果您正在寻找详细信息,请参阅@Tony Vincent 的回答。