【问题标题】:Laravel spatie/laravel-activitylog - Call to undefined relationship [user] on model [Spatie\Activitylog\Models\Activity]Laravel spatie/laravel-activitylog - 调用模型 [Spatie\Activitylog\Models\Activity] 上的未定义关系 [用户]
【发布时间】: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


【解决方案1】:

我设法解决了这个问题。恢复项目旧文件,对比旧Activity.php和当前更新的Activity.php的代码

问题原因: 更新 spatie 包将现有文件替换为新文件,从而删除定义的 我的Activity.php 中的关系函数 Spatie\Activitylog\Models

解决方案:在Activity.php内部定义User模型和Activity模型的关系:

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

【讨论】:

    猜你喜欢
    • 2018-07-09
    • 2018-07-04
    • 2019-03-30
    • 1970-01-01
    • 2023-01-21
    • 1970-01-01
    • 2018-05-27
    • 2019-08-19
    • 2020-09-15
    相关资源
    最近更新 更多