我刚刚更新了我的项目的框架,从第7版到第8版(laravel的最新版本)。除了生成此错误的活动日志外,一切正常:
Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [user] on model [Spatie\Activitylog\Models\Activity].
此错误仅在我更新了laravel和所有composer依赖项(包括spatie/laravel-activitylog )之后才会出现。我按照laravel的指示来更新项目的框架(升级指南)。我还检查了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}";
}
}应该在哪里定义关系和/或如何准确地解决错误?
发布于 2020-11-26 17:10:34
我设法解决了这个问题。恢复项目的旧文件,并将旧Activity.php中的代码与当前更新的Activity.php进行比较。
导致问题的原因:空间包的Spatie\Activitylog\Models.更新用新文件替换了现有文件,从而删除了中My
Activity.php中定义的关系函数
解决方案:定义User模型和Activity模型在Activity.php中的关系
public function user(){
return $this->belongsTo('\App\User', 'causer_id'); //arg1 - Model, arg2 - foreign key
}https://stackoverflow.com/questions/65022727
复制相似问题