Rahmat Effendi
Rahmat Effendi

Reputation: 347

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. But when i am input same data, it changed all data with one value

This is my database, when first insert:

enter image description here

and this is my database, when i am input same data (This the issue):

enter image description here

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:

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

This is decode_data:

enter image description here

How to fix this issue, when i input same data, it not only change all data with one value?

Upvotes: 0

Views: 3313

Answers (2)

Ulaganathan
Ulaganathan

Reputation: 141

You can use this method <model_name>::updateOrCreate() to Create/Update in single method.

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

Upvotes: 1

user10686911
user10686911

Reputation:

You need to specify which record you actually want to update by proving the id in the where clause like this:

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

Upvotes: 2

Related Questions