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

DDSameera's avatar

Spatie Activity Logs in separate database not working

I want to save spatie activity logs in separate database .

database name = "lms_logs"

But it doesn't work. please help me some one to resolve this

config> activitylog.php


..

  
    'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION'),
..

Env


ACTIVITY_LOGGER_ENABLED=true
ACTIVITY_LOGGER_DB_CONNECTION=mysql
ACTIVITY_LOGGER_DB_HOST=127.0.0.1
ACTIVITY_LOGGER_DB_PORT=3308
ACTIVITY_LOGGER_DB_DATABASE=lms_logs
ACTIVITY_LOGGER_DB_USERNAME=root
ACTIVITY_LOGGER_DB_PASSWORD=

Run DB Migration

php artisan migrate:fresh

Clear Config Cache

php artisan config:clear
0 likes
5 replies
sr57's avatar

Why don't you close (Best answer) of your previous post?

DDSameera's avatar
DDSameera
OP
Best Answer
Level 3

activitylog.php

...


   /*
     * This is the database connection that will be used by the migration and
     * the Activity model shipped with this package. In case it's not set
     * Laravel database.default will be used instead.
     */
    'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION'),
...

database.php

  'mysql2' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('ACTIVITY_LOGGER_DB_DATABASE', 'lms_logs'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => 'innodb',
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

env

ACTIVITY_LOGGER_DB_CONNECTION=mysql2
ACTIVITY_LOGGER_DATABASE=lms

actvity_log_migration_table.php

....
    public function up()
    {
        Schema::connection(config('activitylog.database_connection'))->create(config('activitylog.table_name'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('log_name')->nullable();
            $table->text('description');
            $table->nullableMorphs('subject', 'subject');
            $table->nullableMorphs('causer', 'causer');
            $table->json('properties')->nullable();
            $table->timestamps();
            $table->index('log_name');
        });
    }

   ....

1 like
babuyadhu's avatar

@DDSameera I have installed Spatie Activity log. In controller it works fine. but the issue is event name is not getting

activity()->causedBy($user)->withProperties(['log_from' => 'web','type' => 'activity', 'ip' => request()->ip(), 'url' => url()->full()])->log('New permission created by '.$user->name);

So I Define Activity Logs In Model

namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
class Todo extends Model
{
    use LogsActivity;
    protected static $logName = 'todo';
    protected $fillable = ['title', 'description', 'user_id', 'status'];
    
    protected static $logAttributes = ['title', 'description', 'status'];
    public function getDescriptionForEvent(string $eventName): string
    {
        return "This model has been {$eventName}";
    }
}

Nothing is inserting in activity_log table

Please or to participate in this conversation.