Home > Back-end >  How do i set the input ( -, ) based on action type ? laravel
How do i set the input ( -, ) based on action type ? laravel

Time:01-11

How do i set the input ( -, ) based on action type ? laravel first of all look at ss enter image description here I have a form with 5 columns, 5 inputs and i want to insert into my table the "suma" = "amount"(translated) based on what i select on (Tip actiune(0 = cheltuiala, 1 = plata, 2 = incasare)) input form. So if i select 0 and 1 i want to insert into my table the "suma"(amount translated) with - sign operator. if i select option 2, i want to insert it like it is, with ( ). Does anyone knows how to do that?

This is my Controller function

public function saveProjDet(Request $request, IstoricProiecte $istoric, Colaboratori $colaborator)
    {
        $istoric->proiecte_id = $request->input('id_proiect');
        $istoric->action_type = $request->Status_Tranzactii;
        $istoric->colaboratori_id = $request->Colab_id;
        $istoric->suma = $request->input('suma');
        $istoric->data = $request->data;
        $istoric->save();
        return back();
    }

I tried to put (-) on $istoric->suma = $request->input('-suma"); but obviously doesn t work, but i m asking you because i have no idea of how to do that. So the point is to insert into database if my options are (1,2) to insert with (-) sign for "suma" column (which means amount again).

and this is part of my view.blade

<div >
<label >Tip actiune(0 = cheltuiala, 1 = plata, 2 = incasare)</label>
<select  aria-label="Default select example"  name='Status_Tranzactii'>
<option value="cheltuiala">0</option>
<option value="plata">1</option>
<option value="incasare">2</option>
</select>
</div>

CodePudding user response:

When you use something like $request->input('-suma") in your code, PHP looks for a variable literally named -suma, and it doesn't just add the - to the input. In your case, I might try something like this:

if ($request->input('Status_Tranzactii') === 2) {
   $istoric->suma = $request->input('suma');
}
else {
   $istoric->suma = $request->input('suma') * -1;
}

One thing to note: if you want to have a or a - sign in front of each amount, that might be a good case to use an accessor method.

CodePudding user response:

To put something on start of a string, you can use Laravel Helpers. But signs are different based on 0,1,2 condition. Just put them in a condition check them by their value. Like when you select 0, the value of 0 is "cheltuiala" will be sent in controller. So check them by value inside controller and put your necessary code before save.

public function saveProjDet(Request $request, IstoricProiecte $istoric, Colaboratori $colaborator)
    {
        $istoric->proiecte_id = $request->input('id_proiect');
        $istoric->action_type = $request->Status_Tranzactii;
        $istoric->colaboratori_id = $request->Colab_id;
        $istoric->suma = $request->input('suma');

        if ($request->Status_Tranzactii == "cheltuiala" || $request->Status_Tranzactii == "plata") {
           $istoric->suma = Str::start($istoric->suma, '-');
        }
        else {
             $istoric->suma = Str::start($istoric->suma, ' ');;
        }
        $istoric->data = $request->data;
        $istoric->save();
        return back();
    }
  • Related