Answer the question
In order to leave comments, you need to log in
How to correctly build a database structure, where the dependency id may not be?
There are 3 tables.
Transactions(id, transactionable_id, transactionable_type, amount, ...)
Subscriptions(id, ....)
Orders(id, ...)
If it is clear how to create a relationship among these tables. Via morphTo.
How do I register a transaction that doesn't have an "initiator(transactionable)"?
Leave empty fields? Then there will be problems with the output of the list and the violation of dependencies. For example, I need to create a Donation. It makes no sense to create a donation model separately. Because there I again face the same problem. If you make the Donate model, then you need to make the user_id field. Visitors to the site can also donate.
Apparently I'm missing some database design pattern. Please tell me how to deal with this problem.
Answer the question
In order to leave comments, you need to log in
Make optional fields default to null
Simple check on output
@foreach(Transactions::all() as $transaction)
@if (!empty($transaction->transactionable))
{{ $transactions->transactionable->name }}
@endif
@endforeach
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question