-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddPayment.php
63 lines (51 loc) · 1.43 KB
/
AddPayment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace App\Livewire;
use App\Models\Event;
use App\Models\Payment;
use App\Models\User;
use App\Policies\PaymentPolicy;
use Auth;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Livewire\Component;
class AddPayment extends Component
{
public User $payer;
public Event $event;
public ?string $amount = null;
/**
* @var array|string[]
*/
protected array $rules = [
'amount' => 'nullable|numeric',
];
public function render(): Factory|View|Application
{
$paymentSum = $this->payer->sumPaidFor($this->event);
return view('livewire.add-payment', [
'payer' => $this->payer,
'event' => $this->event,
'sum' => $paymentSum,
]);
}
public function updatedAmount(): void
{
$this->amount = str_replace(',', '.', $this->amount ?? '');
$this->validateOnly('amount');
}
public function addPayment(PaymentPolicy $policy): void
{
// Ensure only certain users can create a new payment.
if ($policy->createPayment(Auth::user())->denied()) {
return;
}
$payment = new Payment([
'amount' => $this->amount,
]);
$payment->user()->associate($this->payer);
$payment->event()->associate($this->event);
$payment->save();
$this->amount = null;
}
}