COUPDEGRACES's avatar

image cant loaded , when using laravel jetstream

I show the code like this

 <img class="h-10 w-10 rounded-full" src="{{ Auth::user()->profile_photo_url }}" alt="{{ Auth::user()->name }}" />

when im upload a image, and click open image new tab, thats image not found .

http://web.test/storage/profile-photos/ojd5xGIUsluiHyebteEatRuaVrX5D2OrG6v5Twhm.jpeg

im using localhost:8000 , not using web.test

how i can fix that code?

0 likes
33 replies
Sinnbeck's avatar

What is profile_photo_urlstored as in your database? That full url?

And what is APP_URL set to in .env?

Snapey's avatar

update your APP_URL in your .env file

1 like
COUPDEGRACES's avatar

@snapey already update APP_URL , when im restart artisan serve, and clear cache still same photo url still to web.test

Snapey's avatar

did you run php artisan config:clear ?

2 likes
Sinnbeck's avatar

Did you miss this question?

What is profile_photo_urlstored as in your database? That full url?

COUPDEGRACES's avatar

@sinnbeck sorry , in my database , im using default user from jetstream , and the problem is , when im update photo profile , img not display or not found image.

 <div class="mt-2" x-show="! photoPreview">
                    <img src="{{ $this->user->profile_photo_url }}" alt="{{ $this->user->name }}" class="rounded-full h-20 w-20 object-cover">
                </div>

default code from jetstream

Snapey's avatar

@sinnbeck Jetstream stores profile photos as profile-photos/3RoNPskFEY4KqzBY9ohihRxr2H8a0i7zVVoRe9Gc.jpeg

so, I assume we are talking about one of those. It includes profile_photo_url as an accessor on the user model

    public function getProfilePhotoUrlAttribute()
    {
        return $this->profile_photo_path
                    ? Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path)
                    : $this->defaultProfilePhotoUrl();
    }

So the error could be in the filesystem.php if the default disk has been changed

Snapey's avatar
Snapey
Best Answer
Level 122

@hendrasaputra2323

Your filesystems.php file should contain

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

so the url is what is being to produce the path to the image. You can see here by default it uses APP_URL from the env file directly.

Have you edited this?

2 likes
azc99's avatar

I'm still having this issue using homestead with a fresh install (laravel new l8 --jet). Storage is linked, the config->filesystems.php is not modified, ran php artisan migrate and the default APP_URL is l8.test. In Laravel 7 it was 127.0.0.1.

The profile_photo_path in users table is , i.e. "profile-photos/EAEiRriGl83Qjd7bDCttWKryLq9Zp53irblkYpT6.jpeg". It is being saved, and I can see it with an absolute path in the finder (macos). What should the APP_URL be for the homestead box? As far as I can see, all the solutions work for php artisan serve.

APP_NAME=Laravel8 APP_ENV=local APP_KEY=base64:ayeKbqqYyixOxiy4kmbDfUDBDaOfPI4b/j65fSRUXTQ= APP_DEBUG=true APP_URL=http://l8.test

filesystem.php

Snapey's avatar

@azc99 The APP_URL should be the same as whatever you use to access the site in your browser

2 likes
newbie360's avatar

@azc99

  1. did you add 192.168.10.10 l8.test to your Host system hosts file ?

  2. are you make sure the symbolic link work ? in the Host system go to l8/public , did you see a file called storage and it is point to l8/storage/app/public

  3. after upload a user image, if can't display, view the page source code, check the url

azc99's avatar

So yes, l8.test works in a browser and /etc/hosts has the reference to it.

➜ l8 php artisan storage:link The [/Users/allen/Desktop/Code/sites/12l8/l8/public/storage] link already exists. The links have been created.

the url from the page source looks like it is should be correct:

but the absolute url from the laravel directory does not look correct: /Users/allen/Desktop/Code/sites/12l8/l8/storage/app/public/profile-photos/5FHKxBT5CVF3GxKeDsw2WWjF7WRDa8j0T42IlYKY.jpeg

In the laravel directory the path to the file is: public/storage[sym link to /storage]/app/public/profile-photos/5FHKxBT5CVF3GxKeDsw2WWjF7WRDa8j0T42IlYKY.jpeg

How can I troubleshoot the path better? any issue with homestead? thx!

newbie360's avatar

@azc99 you use command php artisan storage:link inside Homestead

or make the symbolic link in the Host system ?

4 likes
azc99's avatar

well inside homestead storage: link does throw an exception!

vagrant@12l8:~/code/l8$ php artisan storage:link

ErrorException

symlink(): No such file or directory

at vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:315 311▕ */ 312▕ public function link($target, $link) 313▕ { 314▕ if (! windows_os()) { ➜ 315▕ return symlink($target, $link); 316▕ } 317▕ 318▕ $mode = $this->isDirectory($target) ? 'J' : 'H'; 319▕

Any ideas? would my running storage:link in the os instead of homestead cause this?

newbie360's avatar

inside the VM, can't take any action override the Host system

for example, Host system share a folder to VM for read only,

inside the VM is no permission to write files to that folder

go inside Homestead, delete project-root/public/storage if exists

and in the Host system (Mac), make the symbolic link

in Windows can use

mklink /J "D:\path-to\l8.test\public\storage" "D:\path-to\l8.test\storage\app\public"

Mac seems is use ?

ln -s "/path-to/l8.test/public/storage" "/path-to/l8.test/storage/app/public"
JerryBels's avatar

Hello, I have a similar issue, just installed everything on a fresh install (I'm working in a docker) - my app url is http://localhost:60080/ and I registered a user thanks to JetStream. After registering I uploaded a profile picture, and it doesn't show. The url it tries to load is http://localhost:60080/storage/profile-photos/yfdHpOaS24UO9aTUUhGx7W5O3Bwg3urySGIjWqBb.gif but somehow it's not right and I can't find why.

APP_URL=http://localhost:60080
'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],
JerryBels's avatar

@newbie360 thanks for helping. Although, I'm not sure what you would have me do. The link can't be created like that, and doesn't it feel strange that you have to go and make such a link by hand ?

Maybe I should have said that my docker is on a linux os as well (debian).

newbie360's avatar

@jerrybels

look what the command php artisan storage:link actually to do ;)

vendor\laravel\framework\src\Illuminate\Filesystem\Filesystem.php

    /**
     * Create a symlink to the target file or directory. On Windows, a hard link is created if the target is a file.
     *
     * @param  string  $target
     * @param  string  $link
     * @return void
     */
    public function link($target, $link)
    {
        if (! windows_os()) {
            return symlink($target, $link);
        }

        $mode = $this->isDirectory($target) ? 'J' : 'H';

        exec("mklink /{$mode} ".escapeshellarg($link).' '.escapeshellarg($target));
    }
2 likes
agcanta's avatar

@jerrybels I use Docker too and I fix the issue with this command

php artisan storage:link

5 likes
regkeys75!'s avatar

After reading all the post here - this worked for me - I set up a new install of jetstream - checked the .env file looked good and only had to make one adjustment ---> APP_URL=http://localhost:8000 - checked the config/filesystem.php file looked good no adjustments and still did not work .... The one important thing to remember is create the 'SYMLINK' - it will not work no matter what you do until you create this - i created: php artisan storage:link - ALL works now

Hope this helps someone

1 like
Rixton's avatar

I had the same error; I deleted the storage folder in the public directory and ran "php artisan storage:link" command again and it worked.

2 likes
kamrul686's avatar

@Rixton Thanks for this information. This tip helps me. I was searching to solve my profile photo updating in my system. Much information has been gathered. Finally, this one has worked for me.

patake's avatar

I had the same issue with a new Laravel 10 installation. The profile photo was uploading correctly to the profile-photo folder, but i was receiving error 403 when loading ir back to the browser.

The solution:

composer require symfony/filesystem

php artisan storage:link --relative

Please or to participate in this conversation.