updateOrCreate 这个到底咋用的啊

我再提交一次相同的数据,他还是给我插入一条新的数据而不是更新,

文档看得不太懂,

望大佬们 指点下~~

牛魔王的故事
浏览 1177回答 2
2回答

慕侠2389804

很多时候看源码就能明白,源码如下: public function updateOrCreate(array $attributes, array $values = []) { $instance = $this->firstOrNew($attributes); $instance->fill($values)->save(); return $instance; } 参数 $attributes 是 where 条件,根据 $attributes 参数去数据库匹配是否有相关数据,如果没有则新建数据,有就查询出来。 然后再 fill 参数 values 的值去保存。

德玛西亚99

//以id来查找,如果有的话就修改更新,没有的话就create $create_bool =Logistics::updateOrCreate([ 'id' => $model['id'], ],[ 'a' => $model['a'], 'b' => $model['b'], 'c' => $model['c'], 'd' => $model['d'], 'e' => $model['e'], ]);
打开App,查看更多内容
随时随地看视频慕课网APP