如何在laravel中将新数据插入存在行而不替换现有数据?

时间:2019-04-06 03:41:10

标签: php laravel

这里我使用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',
    ]

);

1 个答案:

答案 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',
           ]);