D
D
DenKG2016-12-01 07:00:40
Laravel
DenKG, 2016-12-01 07:00:40

How to update data in database after button click, Laravel?

I have two tables: Purses and Transactions. The Purses table has a purse_balance field. After the transaction is completed, it must be updated, i.e. something is added to the previous value. I wrote the code, but it is of little use, because errors fly out:

public function trans(Request $request){

   $transactions = new Transaction;

   $transactions->trans_pay_sum = $request->trans_pay_sum;
   $transactions->trans_pay_appoint = $request->trans_pay_appoint;
   $transactions->trans_pay_purse_id = $request->trans_pay_purse_id;
   $transactions->trans_pay_purse_client_id = Auth::id();
   $transactions->save();

   $purses = new Purse;
   $purses->purse_name = $request->trans_pay_purse_id;
   $purses->purse_balance = $this->sum($request);
   $purses->save();

    return view('home');
}
  public function sum( Request $request){
    $result = '';
    $result= $request->trans_pay_sum;
    return $result;
  }

Now the error is:
QueryException in Connection.php line 769:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3774817312069704' for key 'purse_name' (SQL: insert into `purses` (`purse_name`, `purse_balance`, `updated_at `, `created_at`) values ​​(3774817312069704, 1000, 2016-12-01 04:00:20, 2016-12-01 04:00:20))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Sashnin, 2016-12-01
@antonsash

purse_name must be unique, hence the error.

A
Anton Perevoshchikov, 2016-12-01
@Fett

Duplicate entry '3774817312069704'
There was the same problem, then either the key went out of range, or the disk space ran out, I don’t remember, but the table was quite large in size. Clearing the table helped.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question