Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将现有数据与新数据合并?_Php_Laravel - Fatal编程技术网

Php 如何将现有数据与新数据合并?

Php 如何将现有数据与新数据合并?,php,laravel,Php,Laravel,方法1:在这里,我编写了将预订座位数据插入数据库的代码 问题:当我预订新座位时,它将创建新行,所以我得到了重复的行,所以我尝试了方法2 方法1代码: $booking = new Bookings(); $booking->users_id = 4; $booking->schedules_id = $schedules_id; $booking->buses_id = $buses_id; $booking-&g

方法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:此处检查
计划id
是否等于存在
计划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


我希望我解释得足够清楚。非常感谢您的帮助。

我不知道这是否是正确的逻辑,但对我来说很有效,欢迎您提出任何其他建议

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

为什么不在座位区使用关系?@stUrb先生,你能解释清楚吗?