0

Currently in Image.php I have this method:

public function getS3Paths()
{
    $this->s3Path = Storage::disk('s3')->temporaryUrl($this->path, now()->addHour());
}

and everytime I need the paths I do something like this:

public function getPhotos(Request $request)
{
    $photos = Image::restricted()->latest()->get();
    foreach ($photos as $photo) {
        $photo->getS3Paths();
    }
    return $photos;
}

I would like to have an accessor in Image.php

public function getS3PathAttribute()
{
    return Storage::disk('s3')->temporaryUrl($this->path, now()->addHour());
}

that would be only called, if I use request similar to $photos = Image::with('s3Path')->restricted()->latest()->get();. Of course with() is for Eloquent and the method would need to be named differently, but it describes my desire well. Is there some solution similar to this, where I would not need to loop the photos? Since s3, is of course paid service, which I don't need everytime I make a request for Photos.

narrei
  • 516
  • 2
  • 6
  • 19
  • yes, thank you very much! should i delete this question? or will it be marked as a duplicate? – narrei Jul 27 '21 at 16:26
  • Also the official solution for this problem is here https://laravel.com/api/8.x/Illuminate/Database/Eloquent/Collection.html#method_append – narrei Jul 27 '21 at 16:29

0 Answers0