如何在laravel中将新数据插入存在行而不替换现有数据? [英] How to insert new data into exists row without replace exists data in laravel?

查看:344
本文介绍了如何在laravel中将新数据插入存在行而不替换现有数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我使用updateOrCreate方法发布数据,但是当我使用此方法时,旧数据替换为新数据,但是我想添加新数据而不更新或替换现有数据.

Here I used updateOrCreate method to post data but when I use this method old data replaced by new data but I want to add new data without updating or replacing exists data.

这是插入数据的代码

$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中,然后与新数据合并,我不知道逻辑是对还是错,但是可以正常工作

I solved myself, I stored old data into $extSeat then merge with new data, I don't know the logic is correct or wrong but works

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

这篇关于如何在laravel中将新数据插入存在行而不替换现有数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆