1 year ago

One to Many - Inverse - Form Select Class component

Posted 1 year ago by Opulido

Hi. I have the following relations in the app I am working on. I have a Bank model or Component as well as Bank Accounts and Currencies. The relation is very simple here. We can have several banks and each bank has multiple bank accounts. Each account has one currency type. Currencies are created with just the name, code and enabled fields (and the classic id and timestamp field). My question is related to the currency type. For a bank account this is not like an ownership type of relationship but more like a configuration parameter. Still, a bank account will have one currency type (belongs to) and a currency could have many bank accounts associated to it. So, even that for me this is a configuration parameter rather than a relationship, I have created the eloquent relationship. Is this the right way to go here?

Also, when I create a new bank account, I need to get all the currency types in order to populate the select component in the blade file. For this, even that the relationship between bankaccount and currency exist (on thinker I can get App\Model\backend\bankaccount::find(1)->currency and App\Model\backend\currency::find(2)->bankaccounts) there is no way to get all the currency types configured (populated in the DB). So in the bankaccount controller in the create method, I do the following: ... $currencies = \App\Model\backend\currency::all(); return view('backend.bankaccount.create',compact('currencies'));

Then on the create.blade.php I simply do:

@foreach ($currencies as $currency) {{ $currency->code }} @endforeach

Is this the way to do so because even that I have the relationship, when creating a new bank account, trough that relationship I can not get all the available currencies configured from the bankaccount model right?



Please sign in or create an account to participate in this conversation.