Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling different database connections #1357

Open
Usman-Ejaz opened this issue Dec 11, 2024 · 1 comment
Open

Handling different database connections #1357

Usman-Ejaz opened this issue Dec 11, 2024 · 1 comment
Labels

Comments

@Usman-Ejaz
Copy link

Describe the bug
I've 2 database connections, One for the main application and other one is for activity logs. Adding data to activity logs table is working fine, but when I'm retreiving data from acvitivut logs table, It is showing the error base table or view secondary_database.users not found. How can I override the causer relation to mention the database connection. I don't want to mention database connection name in each model.

@Usman-Ejaz Usman-Ejaz added the bug label Dec 11, 2024
@felipechiodini
Copy link

felipechiodini commented Dec 11, 2024

I dont know but you can try to overwrite the model class method and force the connection to wished database
make a custom model that extends the model from package Spatie\Activitylog\Models and overwrite the relation method

protected function morphInstanceTo($target, $name, $type, $id, $ownerKey)
{
    $instance = $this->newRelatedInstance(
        static::getActualClassNameForMorph($target)
    );

    if ($your_user_model) {
        $instance->setConnection($whised_connection);
    }

    return $this->newMorphTo(
        $instance->newQuery(), $this, $id, $ownerKey ?? $instance->getKeyName(), $type, $name
    );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants