This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Wouter
Wouter

Hi,

I'm building a blog plugin so I have all my needs. I have one more issue where I can't find any solution for.I'm using tags for this plugin. What I now want to do is to show all post related to a tag.

The relation in the tag model:

public $belongsToMany = [
'posts' => [
            'wouter\Blog\Models\Post',
            'table' => 'wouter_blog_posts'
        ],
];

The relation in de post model:

     public $belongsToMany = [
        'tags' => [
            'wouter\Blog\Models\Tags',
            'table' => 'wouter_blog_posts_tags'
        ],
    ];  

In my components file i try to query the tags to get the related posts

BlogTag::where('slug', '=', 'test-slug')->with('posts')->get();

When above query is running i get a exception but i don't see / understand what i do wrong. I think my relation in the model tag is not right but i can't find what other relation to use.

An exception has been thrown during the rendering of a template ("SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'wouter_blog_posts' (SQL: select wouter_blog_posts.*, wouter_blog_posts.tags_id as pivot_tags_id, wouter_blog_posts.post_id as pivot_post_id from wouter_blog_posts inner join wouter_blog_posts on wouter_blog_posts.id = wouter_blog_posts.post_id where wouter_blog_posts.tags_id in (11))").

I hope someone can help me.

Fibonacci
Fibonacci

I think you should use $hasMany in Post Model. Because the post will be have many tags. And then don't forget to set the 'key' to make sure the model choose the correct column.

jwilson8767
jwilson8767

Change namespaces for classes to Studly case (Wouter\Blog\Models\Tags) and change referenced table in tag model to "wouter_blog_posts_tags"

AlecSPB
AlecSPB
public $belongsToMany = [
'posts' => [
            'Wouter\Blog\Models\Post',
            'table' => 'wouter_blog_posts'
        ],
];
public $belongsToMany = [
        'tags' => [
            'Wouter\Blog\Models\Tag',
            'table' => 'wouter_blog_posts_tags'
        ],
    ];

not to help?

Wouter
Wouter

I have play around al the suggestions.

What i change i just get the follow query.

select * from wouter_blog_tags where slug = 'test-slug' I do something realy wrong

Last updated

AlecSPB
AlecSPB

Wouter said:

I have play around al the suggestions.

What i change i just get the follow query.

select * from wouter_blog_tags where slug = 'test-slug' I do something realy wrong

Pay attention to your mistake: Not unique table/alias: 'wouter_blog_posts'

Mohsin
Mohsin

There is a blog plugin that does this i.e. use tags as belongsToMany. Check this out.

Last updated

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.