RecentResource.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. $title = '';
  25. switch ($this->type) {
  26. case 'article':
  27. $title = Article::where('uid',$this->article_id)->value('title');
  28. break;
  29. case 'chapter':
  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. $para = explode('-',$this->article_id);
  41. if(count($para)===2){
  42. $title = Sentence::where('book_id',(int)$para[0])
  43. ->where('paragraph',(int)$para[1])
  44. ->where('channel_uid',$channelId)
  45. ->value('content');
  46. if(empty($title)){
  47. $title = Sentence::where('book_id',(int)$para[0])
  48. ->where('paragraph',(int)$para[1])
  49. ->where('channel_uid',$paliChannel)
  50. ->value('content');
  51. }
  52. }
  53. break;
  54. default:
  55. $title = $this->article_id;
  56. break;
  57. }
  58. $data['title'] = $title;
  59. return $data;
  60. }
  61. }