【发布时间】:2021-03-09 08:58:36
【问题描述】:
我刚刚将我的项目框架从第 7 版更新到第 8 版(laravel 迄今为止的最新版本)。除了产生此错误的活动日志外,一切正常:
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 类代码:
<?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}";
}
}
我应该在哪里定义关系和/或我应该如何准确地解决错误?
【问题讨论】:
-
你有什么版本的 spatie 活动日志?
-
@N69S 版本 3.16.1(最新版本和支持 Laravel 8 的版本)
-
你怎么称呼模型?
-
@Tpojka 尝试了那个但给了我这个错误:
Symfony\Component\ErrorHandler\Error\FatalError Cannot declare class App\Model\User, because the name is already in use -
已经解决了。我发布了解决方案:) 感谢您尝试帮助我!欣赏它!
标签: php laravel laravel-8 activitylog