ArticleMapResource.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\UserApi;
  5. use App\Http\Api\MdRender;
  6. use App\Models\Collection;
  7. class ArticleMapResource 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. "collect_id" => $this->collect_id,
  20. "article_id" => $this->article_id,
  21. "level" => $this->level,
  22. "title" => $this->title,
  23. "editor"=> UserApi::getById($this->editor_id),
  24. "children" => $this->children,
  25. "deleted_at" => $this->deleted_at,
  26. "created_at" => $this->created_at,
  27. "updated_at" => $this->updated_at,
  28. ];
  29. $channels = [];
  30. if($request->has('channel')){
  31. $channels = explode('_',$request->get('channel')) ;
  32. }else{
  33. $defaultChannel = Collection::where('uid',$this->collect_id)->value('default_channel');
  34. if($defaultChannel){
  35. $channels[] = $defaultChannel;
  36. }
  37. }
  38. $mdRender = new MdRender(['format'=>'simple']);
  39. $data['title_text'] = $mdRender->convert($this->title,$channels);
  40. if ($request->get('view') === 'article') {
  41. $collection = Collection::where('uid',$this->collect_id)->first();
  42. if($collection){
  43. $data['collection']['id']=$collection->uid;
  44. $data['collection']['title']=$collection->title;
  45. }
  46. }
  47. return $data;
  48. }
  49. }