123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Carbon;
- class SiteAlbumLog extends Model
- {
- // 自定义日期序列化格式
- protected function serializeDate(\DateTimeInterface $date)
- {
- return $date->format('Y-m-d H:i:s');
- }
- protected $table = 'site_album_log';
- /**
- * 获取格式化后的日志数据
- *
- * @return array
- */
- public static function getFormattedLogs()
- {
- // 查询数据并按 created_at 降序排序
- $logs = self::orderBy('created_at', 'desc')->get();
- // 初始化结果数组
- $formattedLogs = [];
- // 遍历数据并格式化
- foreach ($logs as $log) {
- // 获取 created_at 的年份和月份(例如:2024-12)
- $monthKey = Carbon::parse($log->created_at)->format('Y年m月');
- // 将数据按月份分组
- if (!isset($formattedLogs[$monthKey])) {
- $formattedLogs[$monthKey] = [];
- }
- // 将日志数据添加到对应月份
- $formattedLogs[$monthKey][] = [
- 'id' => $log->id,
- 'user_name' => $log->user_name,
- 'action' => $log->action,
- 'model' => $log->model,
- 'content' => $log->content,
- 'created_at' => $log->created_at,
- 'updated_at' => $log->updated_at,
- 'content_id' => $log->content_id,
- ];
- }
- return $formattedLogs;
- }
- // 获取下一条日志
- public static function getNextLogEntry($id) {
- // 获取最新的 10 条数据,按 id 降序排列
- $logs = SiteAlbumLog::orderBy('id', 'desc')->take(10)->get();
- if ($logs->count() == 0) {
- return null;
- }
- // 查找 $id 在 $logs 中的位置
- $index = $logs->search(function ($log) use ($id) {
- return $log->id == $id;
- });
- // 如果找到 $id,并且不是最后一条数据,则返回下一条数据
- if ($index !== false && $index < $logs->count() - 1) {
- return $logs[$index + 1];
- }
- // 如果没有找到 $id 或者 $id 是最后一条数据,则返回第一条数据
- return $logs->first();
- }
- }
|