【问题标题】:What is the differences between updateOrCreate() and updateOrInsert() in LaravelLaravel 中 updateOrCreate() 和 updateOrInsert() 有什么区别
【发布时间】:2017-11-08 11:39:39
【问题描述】:

这两种方法似乎都会在不存在的情况下创建新记录或使用提供的数据更新记录。两者有什么区别?

【问题讨论】:

    标签: php laravel laravel-5 eloquent


    【解决方案1】:

    updateOrCreateEloquent Builder 的方法,updateOrInsertQuery Builder 的方法。

    updateOrCreate 返回模型,而 updateOrInsert 返回布尔值


    来自 Laravel 代码的签名:

    updateOrCreate

    /**
     * Create or update a record matching the attributes, and fill it with values.
     *
     * @param  array  $attributes
     * @param  array  $values
     * @return \Illuminate\Database\Eloquent\Model|static
     */
    public function updateOrCreate(array $attributes, array $values = [])
    

    updateOrInsert

    /**
     * Insert or update a record matching the attributes, and fill it with values.
     *
     * @param  array  $attributes
     * @param  array  $values
     * @return bool
     */
    public function updateOrInsert(array $attributes, array $values = [])
    

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 2016-10-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 2015-04-12
      • 2016-10-13
      • 2018-01-09
      相关资源
      最近更新 更多