I have next tables in my Laravel project Posts
id | title |
---|---|
1 | post1 |
2 | post2 |
3 | post3 |
4 | post4 |
5 | post5 |
Tags
id | name |
---|---|
1 | tag1 |
2 | tag2 |
3 | tag3 |
4 | tag4 |
5 | tag5 |
post_tag
post_id | tag_id |
---|---|
1 | 1 |
1 | 2 |
1 | 3 |
1 | 5 |
2 | 2 |
3 | 2 |
3 | 3 |
3 | 5 |
4 | 2 |
4 | 3 |
4 | 4 |
4 | 5 |
5 | 3 |
5 | 4 |
in other words
post | tags |
---|---|
post1 | tag1 tag2 tag3 tag5 |
post2 | tag2 |
post3 | tag2 tag3 tag5 |
post4 | tag2 tag3 tag4 tag5 |
post5 | tag3 tag4 |
I have query - something like that: +tag2+tag3-tag4
(that can be different length; + = include; - = except from result)
How can I get post ONLY with tags which have "+" before and except tags with "-" before?
Using an example table: post1 (has tag2 AND tag3 AND NOT tag4) post3 (has tag2 AND tag3 AND NOT tag4)
I try to use next
$posts = Post::query()
->with('tags')
->whereHas('tags', function ($query) {
$query->whereIn('tags.name', ['tag2', 'tag3'])
->whereNnotIn('tags.name', ['tag4']);
})
->get();
But it doesn't give me needed result Can somebody help me with that??
Thanks!