php - 获取特定模型实例的事件日志(使用 spatie/laravel-activitylog)

标签 php mysql laravel-5

<分区>

我正在使用 spatie/laravel activityLog 包。

activitylog插入代码是

 $user = Auth::user();
 $inventory = Inventory::where('id','=','1')->first();
 $properties = ['action' => 'view'];

 activity()
     ->performedOn($inventory)
     ->causedBy($user)
     ->withProperties($properties)
     ->log('add inventory')
     ->subject('test subject');

如何获取特定模型实例的事件日志?

下面是我的表格

enter image description here

最佳答案

这工作正常

1)回答

 $inventory = new Inventory();
 Activity::where('subject_type',get_class($inventory))->get();

2)回答

 $inventory = new Inventory();
 Activity::forSubject($inventory)->get();

如果你尝试第二个答案那么

您刚刚在此路径中编辑了以下函数 vendor/spatie/laravel-activitylog/src/models/Activity.php

public function scopeForSubject(Builder $query, Model $subject): Builder
    {
        return $query
            ->where('subject_type', $subject->getMorphClass())
            ->orWhere('subject_id', $subject->getKey());
    }

关于php - 获取特定模型实例的事件日志(使用 spatie/laravel-activitylog),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50734994/

相关文章:

mysql - 如何在父表中实现 SQL 外键

php - 从 Wordpress 媒体库获取单个特定图像

php - 存储散列 key : CHAR(64) as datatype in Laravel 4?

php - 从命令行和 max_execution_time 运行 PHP 脚本

MySQL - 字段列表中的未知列

android - 使用内容解析器和内容提供程序时应用程序崩溃

php - 使用 PHP-FFMpeg (Laravel 5) 为视频制作有意义的缩略图

mysql - 使用 Laravel 和 MySQL 比较时间

php - Json 数据错误 : Laravel 5. 1

php - 登录实时服务器时出现 CSS 和导航栏问题