简体   繁体   中英

How to get path from public/images storage symlink?

I have created a symlink to my local storage "(php artisan storage:link)" then, I save images on my public folder like that

public function store(PostStoreRequest $request)
    {
        $validated = $request->validated();
        if($request->hasFile('postImage')) {
            $path = $request->file('postImage')->store('public/images');
            $validated['postImage'] = $path;
        }

        $post = auth()->user()->posts()->create($validated);
        $post->categories()->attach($request->category);
        return redirect()->route('articulos.index');
    }

the $path contains public/images/IdC24xVkoFZiJFWtyKQ2T1xBVrS0GGU3d2Z4NXgP.jpeg for example, it stores the image on storage/app/public/images.

Then in blade I show the image like that:

<img class="card-custom-img-post" src="{{ asset('storage/'.$post->postImage) }}">

It generate this URL: http://localhost/project/public/storage/public/images/IdC24xVkoFZiJFWtyKQ2T1xBVrS0GGU3d2Z4NXgP.jpeg

But the correct one is: http://localhost/project/public/storage/images/IdC24xVkoFZiJFWtyKQ2T1xBVrS0GGU3d2Z4NXgP.jpeg

Anyway to get the correct url without replacing or anything similar?

You have 2 solutions:

You do not save "public/" string in the path (your path will be "images/...").

Or a little workaround:

<img class="card-custom-img-post" src="{{ asset('storage/' . str_replace("public/", "", $post->postImage)) }}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM