| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class PaymentMethod extends Component
- {
- public $records, $name, $enabled, $dataId, $bank_id, $update = false, $add = false;
- public $banks = array();
- protected $rules = [
- 'name' => 'required'
- ];
- protected $messages = [
- 'name.required' => 'Il nome è obbligatorio'
- ];
- public function resetFields(){
- $this->name = '';
- $this->enabled = true;
- }
- public function mount()
- {
- $this->banks = \App\Models\Bank::select('id', 'name')->get();
- }
- public function render()
- {
- $this->records = \App\Models\PaymentMethod::with('bank')->get();
- return view('livewire.payment_method');
- }
- public function add()
- {
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- }
- public function store()
- {
- $this->validate();
- try {
- \App\Models\PaymentMethod::create([
- 'name' => $this->name,
- 'bank_id' => $this->bank_id,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Metodo pagamento creato');
- $this->resetFields();
- $this->add = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore in fase di salvataggio');
- }
- }
- public function edit($id){
- try {
- $payment_method = \App\Models\PaymentMethod::findOrFail($id);
- if( !$payment_method) {
- session()->flash('error','Metodo pagamento non trovato');
- } else {
- $this->name = $payment_method->name;
- $this->enabled = $payment_method->enabled;
- $this->bank_id = $payment_method->bank_id;
- $this->dataId = $payment_method->id;
- $this->update = true;
- $this->add = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore');
- }
- }
- public function update()
- {
- $this->validate();
- try {
- \App\Models\PaymentMethod::whereId($this->dataId)->update([
- 'name' => $this->name,
- 'bank_id' => $this->bank_id,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Metodo pagamento aggiornato');
- $this->resetFields();
- $this->update = false;
- } catch (\Exception $ex) {
- session()->flash('success','Errore');
- }
- }
- public function cancel()
- {
- $this->add = false;
- $this->update = false;
- $this->resetFields();
- }
- public function delete($id)
- {
- try{
- \App\Models\PaymentMethod::find($id)->delete();
- session()->flash('success',"Metodo pagamento eliminato");
- }catch(\Exception $e){
- session()->flash('error',"Errore");
- }
- }
- }
|