I am using spatie/laravel-activitylog to log the activities and jenssegers/laravel-mongodb connect to a Mongo database. In user Model, the user data is storing into mysql and log data need to be stored in mongodb. I am getting "Call to a member function prepare() on null"
config/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's database.default will be used instead.
*/
'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION'),`
database.php
`'mongodb' => [
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => '27017',
'database' => 'test_log',
'username' => '',
'password' => '',
'options' => [
'database' => 'admin'
]
],`
.env
`ACTIVITY_LOGGER_DB_CONNECTION=mongodb`
App\Models\User.php
`<?php
namespace App\Models;
// use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Jenssegers\Mongodb\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
use LogsActivity;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults();
}
} `
In user Model, the user data is storing into mysql and log data need to be stored in mongodb.
Fixed the issue with the help of this tutorial:: https://nickdevs.wordpress.com/2022/05/17/laravel-laravel-activitylog-inegrate-activity-log-with-mongodb/
The issue was with Spatie-activity-log and Jenssegers MongoDB. Spatie-activity-log uses default Model. We need to overwrite the Spatie Activity Model for Jenssegers MongoDB.
Create Activity.php in App/Models. copy the existing code from Spatie\Activitylog\Models\Activity.php from vendor/spatie/laravel-activitylog/src/Models/Activity.php. Replace the lines listed below.
remove:
namespace Spatie\Activitylog\Models;
use Illuminate\Database\Eloquent\Model;
class Activity extends Model implements ActivityContract
add:
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as MongoDBModel;
class Activity extends MongoDBModel implements ActivityContract