1

I'm trying to return query data and storage Images from laravel controller. My code looks like following :

class ClaimController extends Controller{
.....
  public function show(Claim $claim)
  {
    $front_image = Storage::disk('do_spaces')->get($claim->images->front_image);  //image file
    $back_image = Storage::disk('do_spaces')->get($claim->images->back_image);    //image

   // return $front_image   [works]         

    $claim = Claim::all();
   

   //this throws error
   return response()->json([
      'claim' => $claim,
      'images' => [$front_image, $back_image]
   ]);
  }
}

Now I far I understand return->response()->json([]) doesn't send image file. How I can return all data together to frontend app ?

Osman Rafi
  • 938
  • 1
  • 16
  • 37

2 Answers2

2

you have the option to return the images from original server which they are stored in or you can return an encoded version of the image as string and in front end reconstruct it.

$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

And then use it in front like this :

var image = new Image();
image.src = 'data:image/png;base64,iVBORw0K...';
document.body.appendChild(image);

This is the link for second part.

Amir Daneshkar
  • 771
  • 3
  • 9
0

You can use Storage::url()

 $st=\Illuminate\Support\Facades\Storage::disk('do_spaces')->url($claim->images->front_image);

    dd(url($st));

Storage::url() return image path and then url() will return full url path

"http://127.0.0.1:8000/storage/imagefolder/filename"

Make sure to symbolic link to storage folder by running following command

php artisan storage:link

Also you can do the following without storage url

asset('storage/'.$claim->images->front_image)
John Lobo
  • 14,355
  • 2
  • 10
  • 20