dillingham / nova-button

Add buttons on Nova index, detail and lens views.
MIT License
265 stars 33 forks source link

Using buttons in relationship fields that reference the parent #74

Closed goldmerc closed 3 years ago

goldmerc commented 4 years ago

Hi, if I have a HasMany or BelongsToMany relationship field, is there any way I can know the details of the parent?

So, let's say I have a Users resource with a hasMany relationship field to BlogPosts. When I view a user, I can see a table of their BlogPosts. I can use a custom resource for this...

HasMany::make('BlogPosts', null, 'BlogPostsListOnUsersDetailPage')

In the resource BlogPostsListOnUsersDetailPage I want to use the button field and be able to know the id of the related User.

Is this possible? Thanks.

goldmerc commented 4 years ago

I realised I can get what I want from the request and pass it through to a custom event using the button key. Bit hacky but works...

    Button::make('test', $this->getButtonKey())
         ->event(CustomEvent::class)

    public function getButtonKey()
    {
        return implode('@', [
            'viaResource' => request()->viaResource,
            'viaResourceId' => request()->viaResourceId,
        ]);
    }