F
Filamentβ€’10mo ago
gigiloouu

update 2 inputs in live

i have 2 inputs amount and amout_persent. i want when i write amount automaticly fill the amount persent input with some logic.. just how i can make to autimaticly write something to next input?
18 Replies
Dennis Koch
Dennis Kochβ€’10mo ago
Use live() on amount field and ->afterStateUpdated(fn ($set) => $set('amount_persent')
gigiloouu
gigiloouuOPβ€’10mo ago
yep thanks i found souliton im using
->afterStateUpdated(function (Get $get, Set $set) {
self::updateTotals($get, $set);
})
->afterStateUpdated(function (Get $get, Set $set) {
self::updateTotals($get, $set);
})
public static function updateTotals(Get $get, Set $set): void
{
$amount = $get('amount');
$purchase_id = $get('purchase_id');
$a = $purchase_id;
//
// $a = Purchase::where('id', $purchase_id)->first();
// if ($sum_amount != 0) {
// $percentage = ($amount / $sum_amount) * 100;
// } else {
// $percentage = 0;
// }

$amount_formatted = number_format($amount, 2, '.', '');
// $percentage_formatted = number_format($percentage, 2, '.', '');

// $set('amount', $amount_formatted);
$set('amount_percent', $purchase_id);
}
public static function updateTotals(Get $get, Set $set): void
{
$amount = $get('amount');
$purchase_id = $get('purchase_id');
$a = $purchase_id;
//
// $a = Purchase::where('id', $purchase_id)->first();
// if ($sum_amount != 0) {
// $percentage = ($amount / $sum_amount) * 100;
// } else {
// $percentage = 0;
// }

$amount_formatted = number_format($amount, 2, '.', '');
// $percentage_formatted = number_format($percentage, 2, '.', '');

// $set('amount', $amount_formatted);
$set('amount_percent', $purchase_id);
}
but now problem is how i can take sum_amount πŸ˜„ im trying to find best way to how i can take sum_amont from another table speacial column look, in this function i want to get amount_percent It should be equal to whatever percentage of the sum_amount the amount is i have sum_amount in another table and all table have their sum_amount this amount_percent is another table .. and im thinking how i can get actual sum_amount when im creating new table for another table like when i dont know actual id to which row is this for
Dennis Koch
Dennis Kochβ€’10mo ago
i have sum_amount in another table and all table have their sum_amount
Sorry, I can't follow. If you have sum_amount stored in the database in a different table, just load it via Eloquent/DB Facade?
when i dont know actual id to which row is this for
Why? The id should be $record->id?
gigiloouu
gigiloouuOPβ€’10mo ago
no im sorry i know that its to difficult becouse of me okay let me show more detail
gigiloouu
gigiloouuOPβ€’10mo ago
No description
gigiloouu
gigiloouuOPβ€’10mo ago
I have this table named pays there is amount, amount_percent and also purchase_id
gigiloouu
gigiloouuOPβ€’10mo ago
No description
gigiloouu
gigiloouuOPβ€’10mo ago
there is purchases table where i have sum_amount in pays we have purchase_id and with that i can get sum_amount from purchase using purchase_id okay but when im creating new pay for purchase how i can get sum_amount from this purchase becouse its possible to create many pays for one purchase its more clear now ? 😒
Dennis Koch
Dennis Kochβ€’10mo ago
Why do you even store it and don't use Payment::find('your_payment')->pays()->sum('amount'))?
gigiloouu
gigiloouuOPβ€’10mo ago
btw in route i have something like this laso http://127.0.0.1:8000/admin/purchases/1 like last 1 is actual id of purchase if its possible to use there is many pays and everyone has thier own purchase_id so how it will take actual purchase sum amount?
Dennis Koch
Dennis Kochβ€’10mo ago
how i can get sum_amount from this purchase
How is this any different than any other purchase? I think your issue is just, that you need to recalculate the sum
gigiloouu
gigiloouuOPβ€’10mo ago
all purchases has their own sum_amount
Dennis Koch
Dennis Kochβ€’10mo ago
So, either always update the sum after every change (look at Laravel Observers, or Event) or always use ->sum() on a database level
gigiloouu
gigiloouuOPβ€’10mo ago
okay i will try to do with ur advice thanks ❀️ I made it in just pay page but
gigiloouu
gigiloouuOPβ€’10mo ago
No description
gigiloouu
gigiloouuOPβ€’10mo ago
purchase page down i have pays table same as pay page so when i click New pay in there and create new pay how i can take this current purchase id? becouse when i create new i have no idea which purchashe is it
Dennis Koch
Dennis Kochβ€’10mo ago
Is this a relation manager? Then you can access $livewire->ownerRecord
gigiloouu
gigiloouuOPβ€’10mo ago
yep
$this->ownerRecord->id;
$this->ownerRecord->id;
this works now thanks for ur hint πŸ˜„
Want results from more Discord servers?
Add your server