RecentResource.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\ChannelApi;
  5. use App\Models\Sentence;
  6. use App\Models\Article;
  7. class RecentResource extends JsonResource
  8. {
  9. /**
  10. * Transform the resource into an array.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  14. */
  15. public function toArray($request)
  16. {
  17. $data = [
  18. 'id' => $this->id,
  19. 'type' => $this->type,
  20. 'article_id' => $this->article_id,
  21. 'param' => $this->param,
  22. 'updated_at' => $this->updated_at,
  23. ];
  24. switch ($this->type) {
  25. case 'article':
  26. $data['title'] = Article::where('uid',$this->article_id)->value('title');
  27. break;
  28. case 'chapter':
  29. $para = explode('-',$this->article_id);
  30. if(!empty($this->param)){
  31. $param = json_decode($this->param,true);
  32. if(isset($param['channel'])){
  33. $channelId = explode('_',$param['channel'])[0];
  34. }
  35. }
  36. $paliChannel = ChannelApi::getSysChannel('_System_Pali_VRI_');
  37. if(!isset($channelId)){
  38. $channelId = $paliChannel;
  39. }
  40. $title = Sentence::where('book_id',$para[0])
  41. ->where('paragraph',$para[1])
  42. ->where('channel_uid',$channelId)
  43. ->value('content');
  44. if(empty($title)){
  45. $title = Sentence::where('book_id',$para[0])
  46. ->where('paragraph',$para[1])
  47. ->where('channel_uid',$paliChannel)
  48. ->value('content');
  49. }
  50. $data['title'] = $title;
  51. break;
  52. default:
  53. $data['title'] = $this->article_id;
  54. break;
  55. }
  56. return $data;
  57. }
  58. }