我正在使用ActivityLog來記錄我的用戶在Laravel的活動。該軟件包可以非常好地存儲用戶在我使用此軟件包設置的各種型號上執行的所有create
,update
和delete
活動。按spatie活動日誌的自定義屬性搜索laravel
但我現在面臨一個問題。我有ActivityLog
表像如下─
id | log_name | description | subject_id | subject_type | causer_id | causer_type | properties
----------------------------------------------------------------------------------------------------------------------------------------
7 | default | created | 4 | App\Response | 1 | App\User |{"project_id":"22295","feature_id":"2","part_id":"1","response":"yes","user_id":1}
我需要得到這個表由存儲在properties
列project_id
過濾的結果。包文檔說Activity
是一個正常的Eloquent Model
,所以我們可以使用所有Eloquent
提供的默認功能與Activity
模型一起使用。但我不知道如何與Eloquent
做到這一點。
現在,我的代碼如下─
$activities = Activity::latest()->get()->filter(function($item) use($projectId) {
$properties = $item->properties->toArray();
if(isset($properties['attributes']))
$properties = $properties['attributes'];
return ($properties['project_id'] == $projectId);
});
與我上面的代碼的問題達成這個是它獲取所有記錄,直到日期的活動,所以它的加載所有Activity
車型所存在和然後根據project_id
進行篩選。當ActivityLog
的大小增加到大數量時,這將花費大量時間,例如100,000行。有沒有人知道一種更好的方式來獲取基於project_id
的過濾結果,而無需手動通過所有行?