Laravel空间/laravel-activitylog -调用模型[空间\活动日志\模型\活动]上未定义的关系[用户]

hgc7kmma  于 2023-02-17  发布在  其他
关注(0)|答案(2)|浏览(152)

我刚刚将项目框架从Version 7升级到Version 8(目前为止laravel的最新版本),除了活动日志产生以下错误外,其他一切正常:
Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [user] on model [Spatie\Activitylog\Models\Activity].
这个错误只在我更新了laravel和所有composer依赖项(包括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}";
}
}

应在何处定义关系和/或应如何准确解决错误?

xytpbqjk

xytpbqjk1#

我设法修复了这个问题。恢复了项目的旧文件,并将旧的Activity.php的代码与当前更新的Activity.php进行了比较

问题原因:*更新spatie包时,新文件替换了现有文件 *,因此删除了Spatie\Activitylog\ModelsActivity.php内定义的关系函数。

解决方法:在Activity.php中定义**User模型和Activity**模型之间的关系:

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

2vuwiymt2#

在谷歌上发现了同样的问题。
我用的是活动日志4.7,Laravel 9.
不需要向用户添加关系。原因会执行此操作。

延迟加载示例:

检索:Activity::get();
刀片:{{ $entry->causer->name }}
如果延迟加载被禁用

紧急加载示例:

检索:Activity::with('causer')->get();
刀片:{{ $entry->causer->name }}

相关问题