方法1::在这里,我编写了将预订座位数据插入数据库的代码
问题::预订新座位时,它将创建新行,因此我得到重复的行,因此尝试了方法2
方法1代码:
$booking = new Bookings();
$booking->users_id = 4;
$booking->schedules_id = $schedules_id;
$booking->buses_id = $buses_id;
$booking->routes_id = $routes_id;
$booking->seat = implode(',', $seat);
$booking->price = $request->price;
$booking->profile = 'pending';
方法2:这里检查schedules_id
等于存在的schedules_id
,然后更新座位和其他数据
问题:插入新数据以更新旧数据
方法2代码:
$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',
]
);
//我不知道这在逻辑上是正确的还是错误的
我的想法:在这里,我要检索旧数据并将其存储到一个变量中,然后将旧数据和新数据合并到一列中
问题:出现错误。
我的想法代码:
$extSeat = DB::table('bookings')->select('seat')->get();
$booking = Bookings::updateOrCreate(
['schedules_id' => $schedules_id],
[ // update this columns
'buses_id' => $buses_id,
'routes_id' => $routes_id,
'seat' => implode(",", array_merge($seat,$extSeat)),
'price' => $request->price,
'profile' => 'pending',
]);
我实际需要什么? :我需要将现有数据与新数据合并而不进行更新。
旧数据看起来像A1,B1
当插入新数据时,例如C1
我需要像这样的数据A1,B1,C1
我希望我解释得足够清楚。感谢您的任何帮助,谢谢。
答案 0 :(得分:1)
I don't know this is a correct logic or not but works for me, any other suggestions are welcome.
$extSeat = DB::table('bookings')->select('seat')->first();
$extSeat = explode(",", $extSeat->seat);
$booking = Bookings::updateOrCreate(
['schedules_id' => $schedules_id],
[ // update this columns
'buses_id' => $buses_id,
'routes_id' => $routes_id,
'seat' => implode(",", array_merge($seat,$extSeat )),
'price' => $request->price,
'profile' => 'pending',
]);