LogTransformer.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/10
  6. * Time: 17:21
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\PostLog;
  10. use Illuminate\Support\Carbon;
  11. use League\Fractal\TransformerAbstract;
  12. class LogTransformer extends TransformerAbstract
  13. {
  14. public function transform(PostLog $postLog)
  15. {
  16. $type = [
  17. 'add_pv' => '增加浏览量:',
  18. 'add_praise_count' => '增加点赞数:',
  19. 'add_collect_count' => '增加收藏数:',
  20. 'add_share_count' => '增加分享数:',
  21. 'delete' => '删除内容:',
  22. 'update_topic' => '编辑内容话题:',
  23. ];
  24. $data = json_decode($postLog['content']);
  25. $content = '';
  26. foreach($data as $key => $val){
  27. if(!$val) continue;
  28. $content .= $type[$key] . $val .'、';
  29. }
  30. $content = rtrim($content, '、');
  31. return [
  32. 'id' => $postLog['id'],
  33. 'username' => $postLog['username'],
  34. 'log_type' => $postLog['log_type'],
  35. 'created_at' => Carbon::parse($postLog['created_at'])->toDateTimeString(),
  36. 'content' => $content,
  37. ];
  38. }
  39. }