Conditional method based on parent component/route

I want my component to perform 2 different actions based on where the component lives.

Say I have an index page which contains a table with buttons in each modal row. These buttons are displayed using a “buttons” component, which is nested in the “table” component.

Let’s say I also have a “details” page which contains the same buttons for the modal. Now while this page uses the component, it is not nested in any parent component.

Now, let’s say one of these buttons is a “delete” button.

I’m trying to accomplish this:

public function delete($id)
{
    Model::delete();

    if ($this->route == 'models.index') {
        $this->emit('delete-form');
    }
    else {
        return redirect()->route('models.index');
    }
}

This is just an example. So if the delete method is called from the index page, it should emit the event so the parent component is updated. If the delete method is called from the details page, it should redirect the user to the index page.

I’m not sure how to do this. I’ve tried passing the current route name when the buttons component is mounted, but when the parent component is refreshed the mount function comes from a different route.

How would I do this?

Nevermind I think I got it. I just passed a parameter to @livewire for the button component instead of using Route::currentRouteName() in the mount method.