CollectionResource.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\StudioApi;
  5. use App\Models\ArticleCollection;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Resources\Json\JsonResource;
  9. class CollectionResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @param Request $request
  15. * @return array|Arrayable|\JsonSerializable
  16. */
  17. public function toArray($request)
  18. {
  19. $data = [
  20. 'uid' => $this->uid,
  21. 'title' => $this->title,
  22. 'subtitle' => $this->subtitle,
  23. 'summary' => $this->summary,
  24. 'studio' => StudioApi::getById($this->owner),
  25. 'childrenNumber' => ArticleCollection::where('collect_id', $this->uid)->count(),
  26. 'status' => $this->status,
  27. 'lang' => $this->lang,
  28. 'created_at' => $this->created_at,
  29. 'updated_at' => $this->updated_at,
  30. ];
  31. $channel = ChannelApi::getById($this->default_channel);
  32. if ($channel) {
  33. $data['default_channel'] = $channel;
  34. }
  35. $arrList = ArticleCollection::where('collect_id', $this->uid)
  36. ->select(['article_id', 'level', 'title'])
  37. ->orderBy('id')->get()->toArray();
  38. if ($this->fullArticleList === true) {
  39. $data['article_list'] = $arrList;
  40. } else {
  41. $data['article_list'] = array_slice($arrList, 0, 4);
  42. }
  43. return $data;
  44. }
  45. }