use App\Contracts\HasPositions;
use App\Enums\AccountType;
use Illuminate\Database\Eloquent\Model;
class Account extends Model implements HasPositions
{
//
public const TYPES = [
AccountType::CRYPTO => 'Crypto',
AccountType::STOCKS => 'Stocks',
AccountType::CASH => 'Bank'
];
//
}
use App\Enums\AccountType;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Livewire\Component;
class AccountsTable extends Component
{
//
public array $data = [
'name' => '',
'type' => AccountType::STOCKS,
'currency' => 1
];
//
public function createAccount()
{
Validator::make($this->data, [
'name' => ['required', 'string'],
'type' => ['required', Rule::in(Account::TYPES)]
]);
auth()->user()->accounts()->create([
'name' => $this->data['name'],
'type' => $this->data['type'],
'currency_id' => Currency::find($this->data['currency'])->id
]);
return $this->redirect('dashboard');
}
//
}
class AccountType
{
const CASH = 'cash';
const CRYPTO = 'crypto';
const STOCKS = 'stocks';
}
Additional resources on enum:
-
Povilas Korop | www.youtube.com
Published on
-
Povilas Korop | www.youtube.com
Published on