简体   繁体   English

在Laravel中使用foreach更新数据库

[英]Update database using foreach in laravel

I want to insert new data in database using API, but first, i want to check the database using $po_transaction, it exist or not, if $po_transaction exist, do updated. 我想使用API​​在数据库中插入新数据,但首先,我想使用$ po_transaction检查数据库,它是否存在,如果$ po_transaction存在,请进行更新。 But when i am input same data, it changed all data with one value 但是当我输入相同的数据时,它将所有数据更改为一个值

This is my database, when first insert: 这是我的数据库,第一次插入时:

在此处输入图片说明

and this is my database, when i am input same data ( This the issue ): 这是我的数据库,当我输入相同的数据时( 此问题 ):

在此处输入图片说明

This is my controller: 这是我的控制器:

public function post_data(Request $request){

        $po_transaction = $request->input('po_transaction');
        $data = $request->input('data');
        $decode_data = json_decode($data);

        if(!$decode_data){
            return response()->json(['message'=>'No Data','success'=>0]);
        }

        $po_id = Produk::where('po_transaction','=', $po_transaction)->first();

        // if po_id exist, update the data
        if ($po_id) {
            foreach ($decode_data as $item => $value) {
                DB::table('produk')
                    ->where('po_transaction', $po_transaction)
                    ->update(['po_transaction'=>$po_transaction, 'nama_produk'=>$value->produk, 'harga_jual'=>$value->price]);
            }

                return response()->json(['message'=>'success, data saved','success'=>1]);


        }else{
            // if po_id not exist, create new
            foreach($decode_data as $item => $value)
            {
                $saveTransaction = new Produk();
                $saveTransaction->po_transaction = $po_transaction;
                $saveTransaction->nama_produk = $value->produk;
                $saveTransaction->harga_jual = $value->price;
                $saveTransaction->save();
            }
            if($saveTransaction->save()){
                return response()->json(['message'=>'success, data saved','success'=>1]);
            }else{
                return response()->json(['message'=>'no data saved','success'=>0]);
            }
        }
    }

and for data, i am using json data like this: 对于数据,我正在使用json数据,如下所示:

[
  {"produk":"shampoo","price":"12000"},
  {"produk":"noodle","price":"110200"}, 
  {"produk":"cup","price":"1000"}
]

This is decode_data: 这是decode_data:

在此处输入图片说明

How to fix this issue, when i input same data, it not only change all data with one value? 如何解决此问题,当我输入相同的数据时,它不仅会用一个值更改所有数据?

You need to specify which record you actually want to update by proving the id in the where clause like this: 您需要通过证明where子句中的id来指定实际上要更新的记录where如下所示:

DB::table('produk')
  ->where([
    'po_transaction' => $po_transaction, 
    'id_produk'=> $value->id,
  ])
  ->update([
    'po_transaction'=>$po_transaction,
    'nama_produk'=>$value->produk,
    'harga_jual'=>$value->price,
]);

You can use this method <model_name>::updateOrCreate() to Create/Update in single method. 您可以使用此method <model_name>::updateOrCreate()在单个方法中创建/更新。

Produk::updateOrCreate(['po_transaction'=>$po_transaction,'nama_produk'=>$value->produk],['harga_jual'=>$value->price]);

for more info look at this https://laravel.com/docs/5.7/eloquent 有关更多信息, 请参见此https://laravel.com/docs/5.7/eloquent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM