C
C
chelkaz2016-03-20 17:47:11
Laravel
chelkaz, 2016-03-20 17:47:11

Comments on anything. Is there a logic to the finished bike?

I make website services. For example, announcements, questions and answers, and more. Comments are needed everywhere.
I made my own for each section. Your own table. Your model and more. But then I decided to think and ask. It's not better to do this:
Create a single table and a single model. Since the rules of comments are the same everywhere. Just text and that's it.
In the table, make a field of type section id. That is, store all comments in one table, separating them by section id.
How do you think?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavel K, 2016-03-20
@chelkaz

Actually they do it =)

6
65536, 2016-03-20
@65536

one table. hang the rest through morph connection

A
Andrzej Wielski, 2016-03-20
@wielski

We create a table:
id (int), user_id (int), comment (text), commentable_type (string, index), commentable_id (int, index)
And the model below it:

class Comment extends Model
{
  protected $table = 'comments';

  public function commentable()
  {
      return $this->morphTo();
  }
}

We use it as a polymorphic link in the model we need:
public function comments(){
      return $this->morphMany('App\Comment', 'commentable');
}

That's all. All post comments will be in the comments attribute, you can create comments using comments(), edit/delete in the same way.

S
Stanislav Pochepko, 2016-03-20
@DJZT

Polymorphic links?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question