Noel
6 months ago
829
3
Laravel

Retrieve images from database

Posted 6 months ago by Noel

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.

Please sign in or create an account to participate in this conversation.