skip to Main Content

Hello im new to laravel livewire now im trying to sum of two values and result get in another input filed.
but laravel i did this using jQuery now im trying livewire instead of jQuery.

now im getting 1st input value from database table by foreach loop 2nd input value im giving manually inserting, 3rd input im getting result.

problem 1

but im facing problem in 1st input if i use foreach with
wire:model="Amount" & value="{{ $prices->Amount}}" at same time in
input i get empty.

@foreach ($price $prices)
<label>Amount</label>
<input wire:model="Amount" name="Amount" value="{{ $prices->Amount}}">
@endforeach

<label>Disscount</label>
<input type="text" wire:model="Disscount" name="Disscount" >

<label>Result</label>
<input type="text" value="{{ $result }}" name="Disscount" > 
                                                      

problem 2

if i removed code value="{{ $prices->Amount}}" & keeping wire:model="Amount" i dont get value from foreach because i removed value="{{ $prices->Amount}}" this time i entered manually insert value in input it works i get result, but i need to sum value with foreach value.
if i removed wire:model="Amount" & keeping value="{{ $prices->Amount}}" sum not works because value not reach to controller without wire:mode.

@foreach ($price $prices)
<label>Amount</label>
<input wire:model="Amount" name="Amount" value="{{ $prices->Amount}}">
@endforeach

<label>Disscount</label>
<input type="text" wire:model="Disscount" name="Disscount" >

<label>Result</label>
<input type="text" value="{{ $result }}" name="Disscount" >  

my controller

public $result;
public $Amount;
public $Disscount;

public function updated() //used for sum
{
    $this->result = $this->Amount - $this->Disscount;
}

  public function mount()
{
    $this->price = Pricing::all();   
}

2

Answers


  1. You are doing wire:model on one Amount variable but doing that inside of foreach of prices.

    So it’s not entirely clear what exactly you want to mount.

    You shouldn’t use value="..." as Livewire automatically gets the value from public property via wire:model.

    Also you should probably use wire:model on the Result input.

    Login or Signup to reply.
  2. I am not exactly sure why this loop so I’ll show you without loop

    <label>Amount</label>
    <input wire:model="amount" name="Amount">
    
    <label>Disscount</label>
    <input type="text" wire:model="disscount" name="Disscount" >
    
    <label>Result</label>
    <input type="text" wire:model="result" name="Result" >
    

    //component

    public $result;
    public $amount;
    public $disscount;
    
    public function updatedAmount()
    {
        $this->getTotalSum();
    }
    
    public function updatedDisscount()
    {
        $this->getTotalSum();
    }
    
    public function getTotalSum()
    {
        if ($this->amount && $this->disscount)
            $this->result = ($this->amount - $this->disscount)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search