2 years ago

Symlink Confusion: Why does storage:link do this?

Posted 2 years ago by JohnnyW2001

When running php artisan storage:link (as per the documentation), a symlink between public/storage/ to storage/app/public is created. I realise this is by design, but it leads to some very confusing behaviour not covered by the documentation.

For example: If you saveAs a file to an avatars directory, it's stored in: storage/app/avatars and the path returned (for database storage) is: avatars/filename.jpg. This is outside the symlink. Instead we need to tell it saveAs to storage/app/public/avatars (which results in a path returned for database storage being: public/avatars/filename.jpg).

According to the documentation, all we need to do to refer to this file is asset('avatars/filename.jpg') in our code, but that doesn't work. (And neither does the string we stored: asset('public/avatars/filename.jpg').) Instead it needs to be asset('storage/avatars/filename.jpg').

Which means we need to do a str_replace('public', 'storage', asset($filename)) to make an application work as per the documentation's instructions :-/

I assume I'm doing something wrong, but this so very confusing! Is there a better way to deal with this behaviour?

Please sign in or create an account to participate in this conversation.