S
S
Stanislav Pochepko2015-11-03 12:46:56
Laravel
Stanislav Pochepko, 2015-11-03 12:46:56

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

1 answer(s)
V
Vyacheslav Plisko, 2015-11-03
@AmdY

Make optional fields default to null
Simple check on output

@foreach(Transactions::all() as $transaction)
    @if (!empty($transaction->transactionable)) 
        {{ $transactions->transactionable->name }}
    @endif 
@endforeach

If you wish, you can use inheritance, create a Donate model, and from it UserDonate and GouestDonate. The first one will have a user method, the second one will not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question