0

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?

4
  • can you show the value of $decode_data? Commented Feb 13, 2019 at 10:21
  • Currently, you are updating all the row presence inside the table you need a where clause (Maybe a unique value to update specific record ) Commented Feb 13, 2019 at 10:24
  • @AnkurTiwari i have updated my question Commented Feb 13, 2019 at 10:32
  • Add line ->where('nama_produk', $value->produk) after ->where('po_transaction', $po_transaction)`or try to use your Id Commented Feb 13, 2019 at 10:35

2 Answers 2

2

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,
]);
Sign up to request clarification or add additional context in comments.

3 Comments

i don't put id in my json data, so your code is error
I gathered as much, my point is that you need to! Otherwise, your SQL query applies to all records matched in your current WHERE clause.
@RahmatEffendi Please refer this answer, Pass id in your json code and use in your code with where.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.