Infinite relationship...?

Asked

Viewed 54 times

2

I have a question that would be, how to accomplish a relationship that can be infinite.: Ex:

User table with the user id field that would be one for many with Post Table.

Inside the Post Table would have post_id that would have more post inside it.

Table Post with fields ( User_id, Post_id, Text ). Post2 table with fields ( User_id, Post2_id, Post2_postid, Text ).

In the above scenario if you had 10 more posts would use 10 more post tables.

there are some cool ones to have these relationships.

With comparative it would be a tree of directories that inside each one I can have as many directories as you want and files.

1 answer

1


You only need to have a table called post:

id
post_id
user_id
text

When a certain post have no other post inside it, you must set post_id as NULL.

Already in your model insert the following relationships:

public function posts()
{
    return $this->hasMany(Post::class);
}

public function todosOsPosts()
{
    return $this->posts()->with('todosOsPosts');
}

Ref: https://stackoverflow.com/a/26654139/8828706

  • Hello, the answer above worked but a doubt has arisen.. I’m trying to make the post belongs to the post.. example today "post x pertente ao y " se para fazer o inverso " post y depende do x"

  • the reverse would be the belongsTo, know to use ?

  • I’ll take a look Thank you...

  • take a look at this question I asked about this self-rerelacinment... https://answall.com/questions/354430/resultado-relatedment-com-relationship

Browser other questions tagged

You are not signed in. Login or sign up in order to post.