skip to Main Content

How to take the amount from an input and add(+) to the amount table in db? (Laravel)
I have this view :
enter image description here

and i have a table named Collaborators (which has an column named istoric_proiecte_sum_suma(made with withSum() function from laravel, in relationship with another table ProjectHistory that contains details about what collaborator works in any project and how much the owner paid him. and if the collab work on multiple projects the owners has to pay him multiple times, and if that, we have multiple inserts in ProjectHistory table with sums and withSum() function makes the relation between those and add the sum and display it to me) which contains the expenses, payments from the owner of the company to a collaborator) like you see in "suma" column in first image (suma = amount). So if i have a – amount the owner of the company needs to pay the collaborator and he pay him from completing that input and press "realizeaza plata" (translated -> make the payment).

So when he press "make payment" -> realizeaza plata, i need to add (+ sign operator) the amount that i typed in "suma" input to the "suma" (amount) column in first image(which is column istoric_proiecte_sum_suma).
My question is how do i resolve this problem because i have no idea. Look what i’ve tried:

CollaboratorController.php

public function realizeazaPlata(Request $request, Colaboratori $colaborator, $id)
    {
        $colaboratori1 = Colaboratori::findOrFail($id);
        $suma = $request->input('introduSuma');
        dd($suma);
        DB::update('UPDATE Colaboratori SET istoric_proiecte_sum_suma=? WHERE id=?', [($colaborator->istoric_proiecte_sum_suma + $suma), $colaboratori1->id]);
        return back();
    }

ColabsView.blade


<form action="{{ url('colaboratori') }}" method="POST">
@csrf
@method('GET')
<td>
<input type="text" name='introduSuma' placeholder="Suma">
<button type="submit" class="btn btn-primary">Realizeaza plata</button>
</td>
</form>

web.php

Route::get('colaboratori', [AppHttpControllersColaboratoriController::class, 'realizeazaPlata'])->name('realizeazaPlata');

Can someone help me please. Thanks

2

Answers


  1. Do you mean you need to convert negative number to positive and then SUM it?

    You could use abs() function:

    $suma = abs($request->input('introduSuma'));
    

    You should use type=number, to prevent users from entering characters:

    <input type="number" name='introduSuma' placeholder="Suma">
    
    Login or Signup to reply.
  2. You can do it like the following:

    $colaboratori1 = Colaboratori::findOrFail($id); 
    $suma = $request->input('introduSuma');
    
    $colaboratori1->istoric_proiecte_sum_suma = $colaboratori1->istoric_proiecte_sum_suma + $suma;
    $colaboratori1->save();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search