SentenceAttachmentResource.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use Illuminate\Support\Facades\Http;
  5. use App\Models\Attachment;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Support\Facades\App;
  8. class SentenceAttachmentResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @param \Illuminate\Http\Request $request
  14. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  15. */
  16. public function toArray($request)
  17. {
  18. $url = config('app.url') . '/api/v2/attachment/' . $this->attachment_id;
  19. //$response = Http::get($url);
  20. $data = [
  21. 'uid' => $this->uid,
  22. 'sentence_id' => $this->sentence_id,
  23. 'attachment_id' => $this->attachment_id,
  24. 'attachment' => [],
  25. 'editor_id' => $this->editor_id,
  26. ];
  27. $res = Attachment::find($this->attachment_id);
  28. $filename = $res->bucket . '/' . $res->name;
  29. if (App::environment('local')) {
  30. $data['attachment']['url'] = Storage::url($filename);
  31. } else {
  32. $data['attachment']['url'] = Storage::temporaryUrl($filename, now()->addDays(2));
  33. }
  34. return $data;
  35. }
  36. }