CollectionResource.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\StudioApi;
  5. use App\Http\Api\ChannelApi;
  6. use App\Models\ArticleCollection;
  7. class CollectionResource 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. "uid" => $this->uid,
  19. "title" => $this->title,
  20. "subtitle" => $this->subtitle,
  21. "summary" => $this->summary,
  22. "studio" => StudioApi::getById($this->owner),
  23. "childrenNumber" => ArticleCollection::where('collect_id',$this->uid)->count(),
  24. "status" => $this->status,
  25. 'lang' => $this->lang,
  26. "created_at" => $this->created_at,
  27. "updated_at" => $this->updated_at,
  28. ];
  29. $channel = ChannelApi::getById($this->default_channel);
  30. if($channel){
  31. $data['default_channel'] = $channel;
  32. }
  33. if($this->fullArticleList===true){
  34. $data["article_list"] = \json_decode($this->article_list);
  35. }else{
  36. if(isset($this->article_list) && !empty($this->article_list) ){
  37. $arrList = \json_decode($this->article_list);
  38. if(is_array($arrList)){
  39. $data["article_list"] = array_slice($arrList,0,4);
  40. }
  41. }
  42. }
  43. return $data;
  44. }
  45. }