SentenceAttachmentResource.php 1.2 KB

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