RecentResource.php 2.1 KB

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