Search code examples
phplaravelsymlinkpublichomestead

How to access image from storage folder in Laravel Homestead (Symbolic Links problem)?


I am trying to figure out how to access stored files, in this case profile pictures.

Ideally, I want to save them in a "profilepictures" folder. Which can be in the public/profilepictures directory using a symbolic link to storage/app/public/profilepictures

My code right now looks as below:

Controller:

    //store our image in the profilepics directory
    $profilepicpath = request('profilepic')->store('profilepictures', 'public');

    //actually write all the data to the database
    auth()->user()->profile()->create([
        'profilepic' => $profilepicpath,
        'dateofbirth' => $data['dateofbirth'],
        'about' => $data['about'],
        'zipcode' => $data['zipcode'],
        'state' => $data['state']
    ]);

View:

<div class="data" style="visibility: hidden;">
    {{$profile = App\profile::find(Auth::user()->id)}}
</div>
    <div class="profile-wrapper">
        <div class="profile-grid">
            <div class="profilepic">
            <img class="profilepic-image" src={{asset($profile['profilepic'])}}>
            </div>

Filesystems.php in Config:

/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/

'links' => [
    //public_path('storage') => storage_path('app/public'),
    public_path('profilepictures') => storage_path('app/public/profilepictures'),
],

Anyone have any idea why this wouldn't work?

Below I can confirm there is a symbolic link and it appears to be pointing to the right places: Viewing Symbolic Link in Terminal

If I copy the image location from where it should display in view, I get one of the following:

enter image description here

Or

enter image description here

Neither link will work, if I try to visit it in browser I just get error 404. Though based on looking at the directory structure I would think the first should work.

enter image description here enter image description here

Anyone have any suggestions?

I am running laravel on homestead on virtualbox vm installed on ubuntu physical desktop, and so far I have tried many different configurations for this, including editing .htaccess file to no success.


Solution

  • Okay so in this particular instance, the only problem was that when I first ran the php artisan storage:link command, I ran it from my host machine instead of my VM... Since I am using Homestead (vm) all I needed to do was a vagrant destroy, then recreate the VM and run the php artisan storage:link command from the VM terminal and that worked perfectly.