This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
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
aspivot_tags_id
,wouter_blog_posts
.post_id
aspivot_post_id
fromwouter_blog_posts
inner joinwouter_blog_posts
onwouter_blog_posts
.id
=wouter_blog_posts
.post_id
wherewouter_blog_posts
.tags_id
in (11))").
I hope someone can help me.
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.
Change namespaces for classes to Studly case (Wouter\Blog\Models\Tags) and change referenced table in tag model to "wouter_blog_posts_tags"
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?
I have play around al the suggestions.
What i change i just get the follow query.
select * from
wouter_blog_tags
whereslug
= 'test-slug' I do something realy wrong
Last updated
Wouter said:
I have play around al the suggestions.
What i change i just get the follow query.
select * from
wouter_blog_tags
whereslug
= 'test-slug' I do something realy wrong
Pay attention to your mistake:
Not unique table/alias: 'wouter_blog_posts'
1-7 of 7