Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel defining a many-to-many relationship with the same table

So I have a posts table with a corresponding Post model. I want to have related posts for every post. Since a post can have many other related posts, it is a many-to-many relationship between the posts table and the posts table (same table).

So I created a related_posts pivot table with its corresponding model RelatedPost. I want to define this relationship in the two models. Like so:

Post model:

public function related()
{
 return $this->belongsToMany(RelatedPost::class, 'related_posts', 'related_id', 'post_id');
}

RelatedPost model:

public function posts()
{
  return $this->belongsToMany(Post::class, 'related_posts', 'post_id', 'related_id');
}

Now in my post controller after selecting a particular post, I want to get all its related posts. So I do this:

$post->related()->get();

But when I do this I get the following error message:

"SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'related_posts' (SQL: select related_posts.*, related_posts.related_id as pivot_related_id, related_posts.post_id as pivot_post_id from related_posts inner join related_posts on related_posts.id = related_posts.post_id where related_posts.related_id = 1) "

This is my migration for the pivot table:

  Schema::create('related_posts', function (Blueprint $table) {
      $table->increments('id');
      $table->unsignedInteger('post_id');
      $table->unsignedInteger('related_id');
      $table->timestamps();

      $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
      $table->foreign('related_id')->references('id')->('posts')->onDelete('cascade');
  });

I have searched all over everywhere and though the solutions I've found really make sense I haven't been able to get any of them to work.

Any help will be very much appreciated!

like image 733
Awa Melvine Avatar asked Jul 27 '18 06:07

Awa Melvine


People also ask

What is hasMany in Laravel?

hasMany relationship in laravel is used to create the relation between two tables. hasMany means create the relation one to Many. For example if a article have comments and we wanted to get all comments of the article then we can use hasMany relationship .

Does Laravel have many through relations?

The “has-many-through” relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. The first argument passed to the hasManyThrough function is the name of the final model we wish to access, while the second argument is the name of the intermediate model.

What is eager loading in Laravel?

Laravel eager loading. What is eager loading? Eager loading is a concept in which when retrieving items, you get all the needed items together with all (or most) related items at the same time. This is in contrast to lazy loading where you only get one item at one go and then retrieve related items only when needed.


1 Answers

Thanks to @d3jn's comment on my question I was able to solve my problem. So I am posting the solution here just in case someone else might need it.

I am relating the Post model to itself not to the pivot model RelatedPost. So I don't need a RelatedPost model. I only need a pivot table (related_post), and the relation's ids namely related_id and post_id.

So with my migration unchanged, I only need to do away with the RelatedPost model and change my related() method in the Post model to look like this:

public function related()
{
  return $this->belongsToMany(Post::class, 'related_posts', 'post_id', 'related_id');
}

And now everything works.

like image 151
Awa Melvine Avatar answered Oct 18 '22 22:10

Awa Melvine