我刚刚将我的项目框架从第 7 版更新到第 8 版(laravel 迄今为止的最新版本)。除了产生此错误的活动日志外,一切正常:
Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [user] on model [Spatie\Activitylog\Models\Activity].
此错误仅在我更新 laravel 和所有作曲家依赖项(包括spatie/laravel-activitylog
. 我按照 laravel 文档中的说明更新项目框架(升级指南)的步骤。我还检查了spatie/laravel-activitylog
github 上的更新日志,当前最新版本支持 Laravel 8(版本 3.16.1 与我在项目中使用的版本相同)。
这是我的Spatie\Activitylog\Models\Activity
课程代码:
<?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}";
}
}
我应该在哪里定义关系和/或我应该如何准确地解决错误?