O
O
Outsider V.2017-08-08 21:41:19
Laravel
Outsider V., 2017-08-08 21:41:19

What is the best way to organize comments to different modules?

I am making a back-end site on Laravel, I need to file comments on the materials. But the fact is that there is only one comment model, but there are several modules:
1) blog
2) articles
3) perhaps there will be a gallery with comments
I really want to use foreign keys in comments (if anything, the database is PostgreSQL via Eloquent ORM), comments should be linked as "many to one" with:
1) material ID
2) user ID
And here I see two options so far:
1) Make a modules table, where to write the Articles, Blog, Gallery modules and their IDs, then link module->id and comment->module_id with a foreign key. But then it is not clear how to make a foreign key for entry_id, because different materials are in different modules, and id can generally match. As I understand it, according to entry_id, an external link is not possible in this case.
2) Make separate comment tables for different modules. But here there are problems with synchronizing changes in different tables (although you can contrive and solve them by inheritance), with selecting comments for an individual user (the belongsTo trick will no longer work).
Which option is more acceptable, and maybe there are others?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Melikhov, 2017-08-08
@Audiophile

Use a polymorphic relation https://laravel.com/docs/5.4/eloquent-relationship...
Your example is described in the docs. Foreign keys, however, will not. But there is no need to reinvent the wheel.

U
UksusoFF, 2017-08-08
@UksusoFF

Here is another almost finished version: https://github.com/laravelista/comments

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question