GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/html/app/Livewire/Bank/Index.php
<?php

namespace App\Livewire\Bank;

use Livewire\Component;
use App\Models\BankDetail;
use Illuminate\Support\Facades\Session;

class Index extends Component
{
    public $bank_name;
    public $account_name;
    public $account_no;

    public function mount()
    {
        $bank = BankDetail::where('user_id', Session::get('user')['id'])->first();
        if ($bank) {
            $this->bank_name = $bank->bank_name;
            $this->account_name = $bank->account_name;
            $this->account_no = $bank->account_no;
        }
    }

    public function updateBank()
    {
        $userId = Session::get('user')['id'];

        // Validate the input data
        $this->validate([
            'bank_name' => 'required|string|max:255',
            'account_name' => 'required|string|max:255',
            'account_no' => 'required|numeric|digits_between:10,12',
        ]);

        // Use updateOrCreate for create-or-update functionality
        BankDetail::updateOrCreate(
            ['user_id' => $userId], // Match condition
            [
                'bank_name' => $this->bank_name,
                'account_name' => $this->account_name,
                'account_no' => $this->account_no,
            ]
        );
        $this->dispatch('success', 'Bank details updated successfully!');
        $this->redirect('/dashboard-shop');
    }

    public function render()
    {
        return view('livewire.bank.index');
    }
}