archtechx / tenancy

Automatic multi-tenancy for Laravel. No code changes needed.
https://tenancyforlaravel.com
MIT License
3.68k stars 434 forks source link

Laravel Nova Action Log - bug on Tenant resource #931

Closed LucasLavallee closed 2 years ago

LucasLavallee commented 2 years ago

Bug description

We found a bug when using the Action log functionnality of Laravel Nova (https://nova.laravel.com/docs/3.0/actions/defining-actions.html#action-log) and this package.

On our project, we are using Laravel Nova to manage tenants from our central application. We also a Laravel Nova back-office on each tenants. We had the Actionable trait to our Tenant model to add the action log on our resource.

From central, the action log is working properly, displaying the action log on every tenants.

From the back-office of our tenants, we have the possibility to edit some fields of the current tenant(). To do so, we add a custom menu link that redirect on the current tenant edit page (we have one Nova resource for the central and one for the tenants).

The problem is that the data retrieved from the action log on the Tenant resource in our tenants seems to come from the central database and not from the current tenant linked database. It seems that Nova is retrieving the central database data because our tenants table is located on the central database and not in the tenant database.

To avoid this behavior, we have found a way to get around it. We overide the actions method from the Actionable trait on our Tenant model :

/**
     * Get all action events for the tenant.
     */
    public function actions(): MorphMany|Builder
    {
        /*
            Action log: Here we have to override the actions method
            because Nova is using the central action_events on instances.
        */

        if (tenant()) {
            return tenant()->run(function (): mixed {
                return ActionEvent::where('actionable_type', Tenant::class);
            });
        } else {
            return $this->morphMany(Nova::actionEvent(), 'actionable');
        }
    }

Steps to reproduce

Expected behavior

Make sure that Laravel Nova is using the current tenants action_events table data on the Tenant model related resource

Laravel version

8.83.23

stancl/tenancy version

3.5.9

stancl commented 2 years ago

We had the Actionable trait to our Tenant model to add the action log on our resource.

So what's the expected behavior? That the action log data would be inside the tenant DB?

If you use Actionable on the Tenant, then it makes sense that the data will be in the central table, since tenants is a central DB table.