I have two tables: Posts and Pages that share one table for tags (morphToMany).
My tables and relations:
Table: Tags id, name
public function posts()
{
return $this->morphedByMany('AppPost', 'taggable');
}
public function pages()
{
return $this->morphedByMany('AppPage', 'taggable');
}
Table: Posts id, name, active
public function tags()
{
return $this->morphToMany('AppTag', 'taggable');
}
Table: Pages id, name, active
public function tags()
{
return $this->morphToMany('AppTag', 'taggable');
}
How can I get all tags and count from both tables where pages.active and posts.active
I tried this query but this query returns only tags that are in both models:
Tag::whereHas("posts", function($q) {
$q->where("posts.active", "=", 1);
})->whereHas("pages", function($q) {
$q->where("pages.active", "=", 1);
})->get();
I need a query that can return tag if exists in one of the models but where active = 1.