Existed Mocha
Existed Mocha

Reputation: 168

Laravel Add/Append Base URL to Database Result

In my laravel app database, the resource are saved as link like this

/storages/photos/bla-bla.png

what is the best way, so I can append my app url to the result,

www.baseurl.example/storages/photos/bla-bla.png

it's because the backend and front end has difference base url Thanks

Upvotes: 0

Views: 651

Answers (1)

Donkarnash
Donkarnash

Reputation: 12845

IT can be achieved in two ways

  1. While storing the images/files
 $request->photo
    ->storeAs(
        'photos', 
        config('app.url'). $request->file('photo')->getClientOriginalName()
    );
  1. While accessing (via an accessor)

class Some extends Model
{
    public function getPhotoAttribute($value)
    {
        return config('app.url'). $value;
    }

    public function setPhotoAttribute($value)
    {
        $this->attributes['photo'] = str_replace(config('app.url'), '', $value);
    }
}

You need to set the correct value for APP_URL in the .env file

Upvotes: 1

Related Questions