Noel's avatar
Level 1

Retrieve images from database

Hi guys!

I´ve got a PostsController and in the store method I´ve just implemented the posibility to upload an image. I do not have any problem storing the url, in my database I can see correctly the whole path.

 public function store(Request $request)
    {
        try {

            $data = $this->getData($request);

            if($request->file('image_path')) {
                $path = Storage::disk('public')->put('image', $request->file('image_path'));
            }

            Post::create($data);

            return redirect()->route('posts.index')
                ->with('success_message', 'Post was successfully added.');
        } catch (Exception $exception) {

            return back()->withInput()
                ->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']);
        }
    }

I want to use a foreach to show the image that every post includes. I´ve been trying different solutions, but I couldn´t make it.

I changed my storage folder in config/filesystems


    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

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

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

    ],

I created a symbolic link, but nothing it happens at the momente

 @if(! $posts->isEmpty())
                            @foreach ($posts as $post)
                            <div class="post">
                                <div class="user-block">
                                    <span class="username">
                                        <a href="#">{{$post->title}} | {{$post->author}}</a>
                                    </span>
                                    <span class="description">{{optional($post->created_at)->format('d-m-Y')}}</span>
                                </div>
                                <div class="text-center mb-3">
                                    <span>
                                        <img src="{{ asset('storage/image/'.$post->image_path)}}" alt="image_post" height="250">
                                    </span>
                                </div>
                                <p>
                                    {{$post->content}}
                                </p>
                            </div>
                            @endforeach

I think the problem is something related with the path. In the database the path appears like without encrypition, and in the public/image folder all files are encrypted. I don´t have any idea whick image belongs to certain post.

Thanks.

0 likes
3 replies
CorCronje's avatar

Looks as if the "public" directory is missing in the path, could also be an issue with the image's file permissions.

<img src="{{ asset('storage/public/image/'.$post->image_path)}}" alt="image_post" height="250">
Noel's avatar
Level 1

Thanks CorCronje, but it didn´t work. Maybe, you ´re right. I ´ll take into consideration your suggestion.

himanshurajvanshi's avatar

@noel First create full url then check into url(broswer like you project url) that will make sure that you are getting image or not then try it into code.

Please or to participate in this conversation.