Working on a Invoice Generator admin panel for myself and i stumbled on another problem with the accessor. I have found and tried various tutorials for Laravel 8 and Laravel 9, but they all seem to go to the same result.
A company can have multiple bank accounts setup, but when I generate the invoice, I would like for the user to select one bank account to be shown on the invoice. For this I want to show the bank account in a select2 in the format of Bank Account Name(Sort code-Account number).
InvoiceCrudController.php
CRUD::addField([
'name' => 'companies_bank_account_id',
'type' => 'select2',
'label' => 'Bank account',
'tab' => 'Payment',
'entity' => 'bank_account',
'attribute' => 'companies_bank_account_name',
'model' => '\App\Models\CompaniesBankAccount',
'wrapper' => [ 'class' => 'form-group col-md-4' ],
]);
Invoice.php Model
public function getCompaniesBankAccountNameAttribute()
{
return $this->companies_bank_account_name . '(' . $this->companies_bank_account_sort_code . '/' . $this->companies_bank_account_number . ')';
}
Company.php Model
public function bank_accounts() {
return $this->hasMany(CompaniesBankAccount::class, 'company_id');
}
CompaniesBankAccount.php Model
public function company() {
return $this->BelongsToOne(\App\Models\Company::class, 'company_id');
}
So, adding the bank accounts for the company works just fine, but I cannot show multiple attributes to the user using an accessor.
Bank Accounts added to the Company
End result when adding an invoice
If possible to help me spot my mistake or point me to a tutorial, would really appreciate it.
Thanks, George