0

Hi i calculated a discount in my controller by requesting in a form the percentage and the sale_price, so i did this:

public function store(Request $request){
    dd($request->all());
    $presupuestoProducto = new PresupuestoProducto();

    
    $presupuestoProducto->sale_price = $request->sale_price;
    
    $presupuestoProducto->discount_percentage = $request->discount_percentage;
    $presupuestoProducto->discount = ($sale_price * $discount_percentage)/100;
    
    $presupuestoProducto->save();
    Session::flash('success');
    return redirect()->route('presupuestos-productos.view');
}

But now i want to make the result of $presupuestoProducto->discount to appear in a textbox. So it would be like an autocomplete field. So up to now i have it this way in my view.

                   <div class="form-group col-md-2">
                        <label for="sale_price">Precio de Venta</label>
                        <input type="number"  pattern="[0-9]+([\.,][0-9]+)?" step="00.01" name="sale_price" class="form-control">
                    </div>

                    <div class="form-group col-md-3">
                        <label for="discount_percentage">Porcentaje de descuento (%)</label>
                        <input type="number" name="discount_percentage" class="form-control">
                    </div>

                    <div class="form-group col-md-3">
                        <label for="discount">Descuento</label>
                        <input type="number" name="discount" class="form-control" value="discount">
                    </div>

As you can see my last div is an input but i want it to be a text box that automatically appears the result of $presupuestoProducto->discount as i fill in the first two inputs. How should i do this?

matuco1998
  • 53
  • 8
  • It is hard to understand exactly what you're asking. If you want to "live" calculate a percentage based on 2 other input fields, you can use Javascript to do that. [Here's an example](https://stackoverflow.com/q/30922008) with many answers (answers are mostly jQuery but you don't need jQuery for this). This does not involve your PHP code at all, in fact your PHP code which does the same thing is unnecessary, unless you want to recalculate and verify the JS result. It is not "*autocomplete*" either. – Don't Panic Sep 09 '21 at 08:07

1 Answers1

0

you have to use view() and pass the $presupuestoProducto with compact() then you access to the var in your view with it's name

 $presupuestoProducto->save();
return view("ProductView",compact( 'presupuestoProducto'))

in view : ProductView

<div class="form-group col-md-3">
<label for="discount">Descuento</label>
<input type="number" name="discount" placeholder="discount"  class="form-control" value={{$presupuestoProducto->discount}} >
</div>

but if you wanna use route you can try route parameter

hsn u
  • 26
  • 3
  • i tried this with view and passing presupuestoProducto. But then int the input line i get the error $presupuestoProducto is "undefined variable" when i do this value={{$presupuestoProducto->discount}} – matuco1998 Sep 09 '21 at 15:06
  • give me your presupuestoProducto model – hsn u Sep 11 '21 at 10:37