I want to create a weblog using Laravel. Each post has many tags. I have a tag controller with a method to store new tag:
class TagController extends Controller
{
public function store($title){
return Tag::firstOrCreate(['title'=>$title]);
}
}
In this case, i need to call this method from PostController:
class PostController extends Controller
...
protected function createTagsObjects(string $csvTags){
$tagsArray=explode(',',$csvTags);
$tagsArray=array_unique($tagsArray);
foreach($tagsArray as $tag ){
//call to tag controller->store
}
}
}
I know it's not good practice to call controller method from another controller. I surfed the net and I got acquainted with traits and services. But i don't think they are useful for my problem. This scenario happens very often. Could you please help me what is the best practice to handle these situations? Thanks.