Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Using env

What is the difference between the two:

This one shows the right path:

home_slides.blade.php

 <td><img src="{{ env('STORAGE_URL').$item->image }}"></td>

This one does not shows the env('STORAGE_URL') path:

edit_home_slides.blade.php

<td><img src="{{ env('STORAGE_URL').$list->image }}"></td>  

.env

STORAGE_URL=http://localhost/aws_admin_test/storage/app/

I wonder what's wrong with edit_home_slides.blade.php?

Am I allowed to use env in views ? How about in controller?

0 likes
11 replies
ftiersch's avatar

You shouldn't use env in either of those places. Only use env() in your config files and in other places use config() instead and read a config value. That way you can cache your config.

Sinnbeck's avatar

I have written this to you several time. Do NOT use env() outside of config files. Add it to a config file (filestorage.php would be a good place perhaps?)

1 like
davy_yg's avatar
Level 27

I am confused. I only need to show the images from the storage:

 <td><img src="{{ env('STORAGE_URL').$item->image }}"></td>

If I have to create:

config/filestorage.php

<?php

return [

    'storage_url' => env('STORAGE_URL'),

];

It seems like non-standard laravel way. and I don't know how to call the storage_url either?

I saw several websites using env in the controller. Does it waste of memory?

Nakov's avatar

You call it easily as :

config('filestorage.storage_url');

it is for performance reasons.. the config can be cached .env cannot.

1 like
Sinnbeck's avatar

The proper way would be to use the storage helper instead of all that :)

Storage::url($item->image);
1 like
davy_yg's avatar
Level 27

I already tested it. The storage helper only shows /storage/

with $item->image it will ended up like /storage/slides/random-image.jpeg

This is an in correct path. I have to make it:

<img src="{{ env('APP_URL').Storage::url('app/'.$item->image) }}">

to make it works. Another env. See my public/storage does not work yet so that I have to read it directly from the storage/app folders

davy_yg's avatar
Level 27

.env

APP_URL=http://localhost/aws_admin_test
Nakov's avatar

@davy_yg then again, this env('APP_URL') should be replaced with this: config('app.url').

If you want to listen at all :)

1 like
Sinnbeck's avatar

Oh sorry should have been. My bad

Storage::disk('public')->url($item->image);
davy_yg's avatar
Level 27

config/app.php

'url' => env('APP_URL', 'http://localhost'),

Why I have to reset the root folder again in config when it is already set in env? Which one will it reads if I have two different value?

edit_home_slides.blade.php

<tr>
          <td>Image </td>
          <td><img src="{{ config('app.url').Storage::url('app/'.$list->image) }}"></td>          
      </tr>

One thing absurd, image src shows "public/slides/random-image.jpeg"

Nakov's avatar
Nakov
Best Answer
Level 73

You should always read from the config, and the config always reads from the .env file

env('APP_URL', 'http://localhost'),

This means, read what is stored in .env file for the key APP_URL if there is nothing set there, then use http://localhost.

1 like

Please or to participate in this conversation.