I just updated my project's framework from Version 7 to Version 8 (the laravel's latest version as of this date). Everything works except for the activity logs that produces this error:
Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [user] on model [Spatie\Activitylog\Models\Activity].
This error only appears after I updated laravel and all of composer dependencies including spatie/laravel-activitylog
. I followed this instructions from laravel docs for steps in updating the project's framework (Upgrade Guide). I also checked the changelog of spatie/laravel-activitylog
on github and the current latest version supports Laravel 8 (version 3.16.1 the same version I use on my project).
Here's my code for Spatie\Activitylog\Models\Activity
class:
<?php
namespace Spatie\Activitylog\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
class Activity extends Model implements ActivityContract
{
public $guarded = [];
protected $casts = [
'properties' => 'collection',
];
public function __construct(array $attributes = [])
{
if (! isset($this->connection)) {
$this->setConnection(config('activitylog.database_connection'));
}
if (! isset($this->table)) {
$this->setTable(config('activitylog.table_name'));
}
parent::__construct($attributes);
}
public function subject(): MorphTo
{
if (config('activitylog.subject_returns_soft_deleted_models')) {
return $this->morphTo()->withTrashed();
}
return $this->morphTo();
}
public function causer(): MorphTo
{
return $this->morphTo();
}
public function getExtraProperty(string $propertyName)
{
return Arr::get($this->properties->toArray(), $propertyName);
}
public function changes(): Collection
{
if (! $this->properties instanceof Collection) {
return new Collection();
}
return $this->properties->only(['attributes', 'old']);
}
public function getChangesAttribute(): Collection
{
return $this->changes();
}
public function scopeInLog(Builder $query, ...$logNames): Builder
{
if (is_array($logNames[0])) {
$logNames = $logNames[0];
}
return $query->whereIn('log_name', $logNames);
}
public function scopeCausedBy(Builder $query, Model $causer): Builder
{
return $query
->where('causer_type', $causer->getMorphClass())
->where('causer_id', $causer->getKey());
}
public function scopeForSubject(Builder $query, Model $subject): Builder
{
return $query
->where('subject_type', $subject->getMorphClass())
->where('subject_id', $subject->getKey());
}
}
And here's my code for User
Model:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use Notifiable, LogsActivity, HasRoles;
protected static $ignoreChangedAttributes = ['password', 'updated_at'];
protected $fillable = [
'first_name', 'last_name', 'gender', 'birthdate', 'user_contact', 'status', 'email', 'password',
];
protected static $logAttributes =
[
'first_name', 'last_name', 'gender', 'birthdate', 'user_contact', 'status', 'email', 'password',
];
protected static $logOnlyDirty = true;
protected static $logName = 'User';
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function getDescriptionForEvent(string $eventName): string
{
return "A user has been {$eventName}";
}
}
Where should I define the relationship and/or how exactly should I solve the error?
I managed to fix the problem. Recovered the old files of the project and compared the code from the old Activity.php
to the currently updated Activity.php
Cause of the Problem: Updating of spatie package replaced the existing file by the new ones thus removing the defined relationship functions inside my
Activity.php
in Spatie\Activitylog\Models.
Solution: Define the relationship between the User
model and Activity
model inside the Activity.php
:
public function user(){
return $this->belongsTo('\App\User', 'causer_id'); //arg1 - Model, arg2 - foreign key
}