我正在尝试了解larvel 8新功能更新,它看起来非常不错
波纹管是我的样品桌
flights
id (primary key and auto Inc)
departure
destination
price
in my example
App\Models\Flight::upsert([
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['departure', 'destination']);
这是Laravel文档中的示例表,具有一个键ID。如果“出发地”和“目的地”都匹配,但这些字段不是唯一的,我想更新记录。
每次我运行代码时,它都会插入新记录,但不会更新。如何获得上岗工作,我需要同时使“出发地”和“目的地”两者都唯一,或者如果我不需要使这两个领域都唯一,那么它也将不具有唯一性就可以工作,那么我该如何在迁移中做到这一点。
谢谢
我相信您需要传递第二个参数中的唯一键,该键的第二个参数upsert
是表的id列和主键,示例数据将是
App\Models\Flight::upsert([
['id'=> 1, 'departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['id'=> 2, 'departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['id']);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句