问答 / 0 / 4 / 创建于 3年前 / 更新于 3年前
1,如果没有数据,就插入数据。2,如果有数据,就对这个数据的日期(某个日期字段,比如end_at),增加1个月。
end_at
我想的是使用updateOrCreate(),但是不知道怎么实现上面的第二个需求。请教各位大神。有办法实现吗?
updateOrCreate()
update
create
可以用firstOrNew,然后判断是否存在,存在就加一个月,不存在就save创建
firstOrNew
可以在观察者 updating 方法中进行判断
return tap(abc::query()->firstOrNew($where), function ($instance) use ($data) { if (!$instance->isDirty()) { $instance->fill($data)->save(); } });
我要举报该,理由是:
推荐文章: