PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Laravel教程 > 正文

    Laravel里firstOrCreate、firstOrNew、updateOrCreate 方法使用

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:在日常开发的过程中,经常会遇到判断一条记录是否存在、存在更新、不存在新建记录这种场景,在Laravel中提供了方法支持,那么下面就看下具...
    在日常开发的过程中,经常会遇到判断一条记录是否存在、存在更新、不存在新建记录这种场景,在 Laravel 中提供了方法支持,那么下面就看下具体的方法;

    使用时请注意版本,下面介绍的函数 firstOrCreate 和 firstOrNew 跟版本有很大的关系

    firstOrCreate

    firstOrCreate 方法将会使用指定的字段 => 值对,来尝试寻找数据库中的记录。如果在数据库中找不到,5.5 以下版本会使用属性来添加一条记录,5.5 及以上版本则将使用第一个参数中的属性以及可选的第二个参数中的属性插入记录

    用法:

    1
    2
    
    User::firstOrCreate(['name' => 'Lisi']);
    User::firstOrCreate(['name' => 'Lisi'], ['age' => 20]);  // 5.5及以上版本支持
    

     

    查看源码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    # 小于 5.5 版本,只有一个参数
    public function firstOrCreate(array $attributes)
    {
        if (! is_null($instance = $this->where($attributes)->first())) {
            return $instance;
        }
        $instance = $this->model->newInstance($attributes);
        $instance->save();
        return $instance;
    }
    
    # 5.5 版本
    public function firstOrCreate(array $attributes, array $values = [])
    {
        // 判断是否存在,如果存在,返回实例
        if (! is_null($instance = $this->where($attributes)->first())) {
            return $instance;
        }
    
        // 不存在创建,此代码简化就是 $this->newModelInstance($attributes + $values)->save();
        return tap($this->newModelInstance($attributes + $values), function ($instance) {
            $instance->save();
        });
    }
    

     

    firstOrNew

    会尝试使用指定的属性在数据库中寻找符合的纪录。如果未被找到,将会返回一个新的模型实例。请注意 firstOrnew 返回的模型还尚未保存到数据库。你需要通过手动调用 save 方法来保存它

    用法:

    1
    2
    
    User::firstOrNew(['name' => 'Lisi']);
    User::firstOrNew(['name' => 'Lisi'], ['age' => 20]); // 5.5及以上版本支持
    

     

    查看源码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    # 小于 5.5 版本,只有一个参数
    public function firstOrNew(array $attributes)
    {
        if (! is_null($instance = $this->where($attributes)->first())) {
            return $instance;
        }
        return $this->model->newInstance($attributes);
    }
    
    # 5.5 版本
    public function firstOrNew(array $attributes, array $values = [])
    {
        if (! is_null($instance = $this->where($attributes)->first())) {
            return $instance;
        }
        return $this->newModelInstance($attributes + $values);
    }
    

     

    查看源码就更清楚 firstOrCreate 比 firstOrNew 多了 save 方法,两个方法都很实用,根据场景使用它。

    updateOrCreate

    更新数据,如果不存在则创建,这个函数就充分利用到了方法 firstOrNew,此函数版本之间变化不大

    用法:

    1
    
    User::updateOrCreate(['name' => 'Lisi'], ['age' => 20]);
    

     

    查看源码:

    1
    2
    3
    4
    5
    6
    7
    
    # 5.5 版本
    public function updateOrCreate(array $attributes, array $values = [])
    {
        return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
            $instance->fill($values)->save();
        });
    }
    

     

    总结

    firstOrCreate:判断之后直接入库
    firstOrNew:判断之后还要做其他业务流程,之后再入库
    updateOrCreate:更新数据,如果不存在则创建

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-153-6225-1.html
    相关热词搜索: