Search code examples
phpmysqllaravellaravel-blade

laravel storing images on database with full path and I cant retrieve


I'm making a site where you can store files by a form, and after it'll show on a page.

this is my form to insert:,

 <form action="{{ route('post.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="bg-grey-lighter pt-15">
                    <label
                        class="w-44 flex flex-col items-center px-2 py-3 bg-white-rounded-lg shadow-lg tracking-wide uppercase border border-blue cursor-pointer">
                        <input type="file" name="thumb" class="hidden">
                    </label>
                </div><br>

this is the code I'm using to show the image:

            <img src="{{  asset('/images/posts/' . $post->thumb }}" class="img-post" />
            <br>

this is my PostController: (thumb is the name of the field on database)


    public function update(Request $request, Post $post, $id)
    {


        $post = Post::findOrFail($id);
      
        $path = public_path('images/');
        $imageName = time(). '.' . $request->thumb->extension();  
        $post->thumb = $request->thumb->move($path, $imageName); 


            $request->validate([
                'thumb' => 'mimes:jpeg,png,jpg,gif',
            ]);
    

        $post->update();
        return redirect()->back()->with('success', 'updated');
    }

and in the database is storing like this:

C:\Users\users name\Documents\laravel\projeto-qbv2\public\images/posts\1700092184.png

and in laravel the images are storing in:

public\images\posts

tried already to change the function, the img src...


Solution

  • you need to use laravel file storage system.

    first of all you need to configure your storage symbolic link. you can do it by running this commande in artisan CLI:

        php artisan storage:link
    

    You may configure additional symbolic links in your filesystems configuration file. Each of the configured links will be created when you run the storage:link command:

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

    After that, You can simply use the storage functions:

    your PostController.php code would be like this :

    public function update(Request $request, Post $post, $id)
    {
    
    
        $post = Post::findOrFail($id);
        $request->validate([
                'thumb' => 'mimes:jpeg,png,jpg,gif',
            ]);
        if ($request->hasFile('logo')) {
    
        $path = $request->logo->store('/images');
        $post->thumb = $path;
    
        $post->update();
        return redirect()->back()->with('success', 'updated');
        }
      
        
    }
    

    this is the code you should be using to show the image:

            <img src="{{  asset('/storage/' . $post->thumb }}" class="img-post" />
            <br>
    

    For more information about this i recommend Laravel Documentation : https://laravel.com/docs/10.x/filesystem