这里我使用updateOrCreate
方法发布数据,但是当我使用此方法时,旧数据替换为新数据,但是我想添加新数据而不更新或替换现有数据。
这是插入数据的代码
$booking = Bookings::updateOrCreate(
['schedules_id' => $schedules_id], // match the row based on this array
[ // update this columns
'buses_id' => $buses_id,
'routes_id' => $routes_id,
'seat' => json_encode($seat),
'price' => $request->price,
'profile' => 'pending',
]
);
答案 0 :(得分:1)
我解决了自己,我将旧数据存储到$ extSeat中,然后与新数据合并,我不知道逻辑是对还是错,但是可以工作
$extSeat = DB::table('bookings')->select('seat')->first();
$extSeat = explode(",", $extSeat->seat);
$booking = Bookings::updateOrCreate(
['schedules_id' => $schedules_id],// row to test if schedule id matches existing schedule id
[ // update this columns
'buses_id' => $buses_id,
'routes_id' => $routes_id,
'seat' => implode(",", array_merge($seat,$extSeat )),
'price' => $request->price,
'profile' => 'pending',
]);