Laravel spatie/laravel-activitylog - 调用模型 [Spatie\Activitylog\Models\Activity] 上的未定义关系 [user]

Laravel spatie/laravel-activitylog - Call to undefined relationship [user] on model [Spatie\Activitylog\Models\Activity]

我刚刚将我的项目框架从版本 7 更新到版本 8(laravel 截至该日期的最新版本)。除了产生此错误的 activity 日志外,一切正常:

Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [user] on model [Spatie\Activitylog\Models\Activity].

此错误仅在我更新 laravel 和包括 spatie/laravel-activitylog 在内的所有作曲家依赖项后出现。我按照 laravel 文档中的说明更新项目框架 (Upgrade Guide)。我还在 github 上检查了 spatie/laravel-activitylog 的变更日志,当前最新版本支持 Laravel 8(版本 3.16.1 与我在项目中使用的版本相同)。

这是我的代码 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());
}
}

这是我的 User 型号的代码:

<?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?

我设法解决了这个问题。恢复项目的旧文件并将旧 Activity.php 中的代码与当前更新的 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.

解决方法:定义User模型与Activity模型之间的关系Activity.php:

public function user(){
    return $this->belongsTo('\App\User', 'causer_id'); //arg1 - Model, arg2 - foreign key
}