我正在尝试了解 Laravel 8 的新功能upsert
.
这是我的示例表:
flights
id (primary key and auto Inc)
departure
destination
price
在我的代码中:
App\Models\Flight::upsert([
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['departure', 'destination']);
这是 Laravel 文档中的示例表,它有一个键 id。
我想更新记录,如果两者都departure
and destination
是匹配的,但这些字段不是唯一的。
每次我运行代码时,它都会插入一条新记录但不会更新。如何获得upsert
在职的?
我需要两者都做吗departure
and destination
独特还是会在不使它们独特的情况下发挥作用?
另外,如果我需要使这两个字段都唯一,那么我该如何在迁移中做到这一点?