Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PaulE's avatar
Level 1

Allow creation of relationship resource

In Nova I have two models;

  • Post
  • Tag

Both resources should be visible in the menu, however, tags are not allowed to be created. My TagPolicy has public function create() { return false; } which prevents the creation of tags from directly accessing the resource.

Now I want my Posts to have a tag, so I have added the relation to the fields. My tags show up, but the create button is hidden due to the TagPolicy previously defined.

From the docs I get the idea that it should be possible to add public function addTag to my PostPolicy in order to allow/deny the creation of a tag on my post, however, this only works if the create is allowed for tags from the TagPolicy.

Is there any way how I can bypass this logic and be able to define the policy so that I can only create a tag from the Post?

--

P.S. I know in the case of Post/Tags it should be Many to Many (attach, detach), but this is just an example

0 likes
3 replies
bobbybouwmann's avatar

Mmh good question... You should be able to return true for the create method based on the current URL. It's not the best solution, but I think it's the only solution here. Nova isn't built for these complex situations most of the time. Nova works for 90% of the use cases ;)

Any, you might be able to do this

public function create()
{
    if (request()->is('admin/resources/posts/create')) {
        return true;
    }

    return false;
}

Not sure if this is the URL, but you get the idea ;)

PaulE's avatar
Level 1

Unfortunately the policies are pre-loaded, so this only works upon a reload of the page. So for instance, if I go the main page of nova the create will return false and stay that way until I refresh on the posts/create page.

bobbybouwmann's avatar

Mmh, that is annoying. I don't know any other workaround for it at this point.

Please or to participate in this conversation.