Darren Lau
Darren Lau

Reputation: 2055

Laravel - Define path variable

how do I define path variable in laravel? For example, I have a webserver path https://s3-ap-southeast-1.amazonaws.com/profile_image

in my HTML I will have:

   <img style="width: 100%;padding:1px;border: 2px solid #555" src="https://s3-ap-southeast-1.amazonaws.com/myapps/profile_image/{{ $data->ap_thread_created_by}}/{{ $data->image_path }}">

but I wish to get the path in my config or somewhere else.. instead of hardcoding the path.

Upvotes: 0

Views: 1907

Answers (3)

Nay Zaw Oo
Nay Zaw Oo

Reputation: 511

function avatar_url($data) {
    return "https://s3-ap-southeast-1.amazonaws.com/myapps/profile_image/{$data->ap_thread_created_by}/{$data->image_path}";
}
// src="{{avatar_url($data)}}"

Upvotes: 0

Kuki Amarilla
Kuki Amarilla

Reputation: 66

You can define environment variable in .env file:

IMAGES_PATH=https://s3-ap-southeast-1.amazonaws.com/myapps/profile_image

And get it in any place this way:

<img style="width: 100%;padding:1px;border: 2px solid #555" src="{{env('IMAGES_PATH')}}/{{ $data->ap_thread_created_by}}/{{ $data->image_path }}">

Upvotes: 1

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111889

Assuming $data is representation of your model for example Photo you can add accessor to it:

public function getStoragePathAttribute($value) 
{
   return 'https://s3-ap-southeast-1.amazonaws.com/myapps/profile_image/';
}

and now in your blade you can use:

<img style="width: 100%;padding:1px;border: 2px solid #555" src="{{ $data->storage_path }}{{ $data->ap_thread_created_by}}/{{ $data->image_path }}">

Upvotes: 0

Related Questions