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}}"
& keepingwire:model="Amount"
i dont get value from foreach because i removedvalue="{{ $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 removedwire:model="Amount"
& keepingvalue="{{ $prices->Amount}}"
sum not works because value not reach to controller withoutwire: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
You are doing
wire:model
on one Amount variable but doing that inside offoreach
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 viawire:model
.Also you should probably use
wire:model
on the Result input.I am not exactly sure why this loop so I’ll show you without loop
//component