Adding the same data in another pivot table (Adding field to Blogpost which use data of tags)

I add a field like tag to my BlogPost & Blog by using Blogtag.php here is my changes for my new field:

  • 1.adding the 'Subs' => BlogTag::class to has_many in my Blog.
  • 2.adding the 'Subs' => BlogTag::class to many_many in BlogPost.
  • 3.adding $subs = $parent instanceof Blog ? $parent->Subs() : BlogTag::get(); to BlogPost.
  • 4.adding TagField::create('Subs', _t(__CLASS__ . '.other', 'other tags'), $subs, $this->Subs())->setCanCreate($this->canCreateSubs())->setShouldLazyLoad(true) to BlogPost.
  • 5.adding public function canCreateSubs($member = null){$member = $this->getMember($member); $parent = $this->Parent(); if (!$parent || !$parent->exists() || !($parent instanceof Blog)) {return false;} if ($parent->isEditor($member)) {return true;} if ($parent->isWriter($member)) {return true;} return Permission::checkMember($member, 'ADMIN');} to BlogPost.
  • 6.adding foreach ($this->Subs() as $sub) {$sub->BlogID = $this->ParentID; $sub->write();} to BlogPost.

After these i could loop the $Subs in template and the Subs create in the ‘blogtag’ table and i have a new pivot table named ‘blogpost_subs’ but i need to create that data(blogpost_subs row) in my ‘blogpost_tags’ table and when i click a tag or a sub both of that show in a page (domainname.com/blog/tags/test : it contains the posts which tag is test and the sub is test)

1 Like