TransWikia.com

Retrieving image from database and showing it Laravel

Stack Overflow Asked by Parsa_237 on January 22, 2021

I am making a web application in the Laravel Framework. I have written code that saves a image in the database.

Code for saving a image in the database (code in my controller):

public function store(Request $request)
    {       
        $request->validate([
            'title' => 'required',
            'description' => 'required',
            'category_id' => 'required',
        ]);
  
        $user = request()->user();
        $assortment = $user->assortments()->create($request->all());


        if($request->hasFile('image') && $request->file('image')->isValid()) {
            if($request->hasFile('image') && $request->file('image')->isValid()) {
                //dd($request->image->store('images', 'public'));
                $assortment->image_path = $request->image->store('images', 'public');
                $assortment->save();
                
                //Check what the Storage::url() generates
                //dd(Storage::disk('public')->url($assortment->fresh()->image_path));
                }
                else {
                abort(500, "Image not valid");
                }
            }
        
         if ($request->wantsJson()) {
             return response([], 204);
        }

        return redirect()->route('assortments.show', $assortment->slug)
                        ->with('success','Verzameling is succesvol aangemaakt.');

        
    }

I want my image to be retrieved from my database in the view but I do not know how to go about doing this. I tried things like:

<img src="/images/{{ $imageVariableName }}"> Variables and path are changed

The view I am trying to make my image be retrieved and shown:

<div class="mt-10">
    <ul class="md:grid md:grid-cols-2 md:gap-x-8 md:gap-y-10">
        @foreach ($assortment->itemCategories as $category)
            <li>
                <a href="{{ route('categories.show', [$assortment->slug, $category->id]) }}">
                    <div class="flex px-4 py-5 bg-white shadow overflow-hidden sm:rounded-md sm:px-6">
                        <div class="flex-shrink-0">
                            <div class="flex items-center justify-center h-12 w-12 rounded-md bg-indigo-500 text-white">
                                <!-- Heroicon name: globe-alt -->
                            </div>
                        </div>
                        <div class="ml-4">
                            <h4 class="text-lg leading-6 font-medium text-gray-900">{{ $category->name }}</h4>
                        </div>
                    </div>
                </a>
            </li>
        @endforeach
    </ul>
</div>

Anyone know how I go about doing this?

EDIT

I also tried:

<img src="{{ Storage::disk('public')->url($assortment->image_path) }}">

But it also returned no image. Just returned:

enter image description here

I inspected the image and it returned the following link:

http://localhost/storage/tmp/phpSX2W3G

EDIT

I know now the problem has to do with my upload controller. The file should not be stored at a temporary path: /storage//tmp/phpL1ZVcx

EDIT 2

The storage link appears to be broken. Get this in my public/storage:

enter image description here
Greetings,

Parsa

5 Answers

You can try the below:

Have included comments in the code with dd() statements to debug - uncomment the dd() statements if you don't get it working to check.

public function store(Request $request)
{       
    $validated = $request->validate([
        'title' => 'required',
        'description' => 'required',
        'category_id' => 'required',
    ]);
  
    $user = request()->user();

    if(!$user) {
        abort(401, 'Unauthenticated');
    }

    $assortment = $user->assortments()->create($validated);


    if($request->hasFile('image') && $request->file('image')->isValid()) {
        //dd($request->image->store('images', 'public'));
        $assortment->image_path = $request->image->store('images', 'public');
        $assortment->save();
                
        //Check what the Storage::url() generates
        //dd(Storage::disk('public')->url($assortment->fresh()->image_path));
    } else {
        abort(422, "Image not valid");
    }
            
        
    if ($request->wantsJson()) {
        return response([], 204);
    }

    return redirect()->route('assortments.show', $assortment->slug)
        ->with('success','Verzameling is succesvol aangemaakt.');

        
}

Verify that you have a public disk configured in the configfilesystems.php, something like

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

//And links specified for symlinking when php artisan storage:link is run

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

Then run

php artisan storage:link

and check in the public folder that there exists a symlinked storage folder

Then in your blade view, you can use

<img src="{{ Storage::disk('public')->url($assortment->image_path) }}" />

//OR

<img src="/storage/{{ $assortment->image_path }}" />

Answered by Donkarnash on January 22, 2021

Try a much simplified version:

if ($request->hasFile('image')) {
    $path = $request->file('image')->storePublicly('images'); // Store file in public disk & return path
    $assortment->image_path = $path; // If you want filename only, use basename($path)
    $assortment->save();
}

After that, you can use Storage::disk('public')->url($assortment->image_path) in your view. Using getClientOriginalExtension() and getClientOriginalExtension() probably isn't a good idea since it's not considered a safe value (source). Laravel's store method saves the file and returns the hashed filename with path for you so you don't have to worry about all that :)

Make sure storage symlink is created:

php artisan storage:link

Also make sure post_max_size and upload_max_filesize in your php.ini are set with sufficient amount.

Answered by sykez on January 22, 2021

Try <img src="{{ url('storage/images/'.$assortment->image_path) }}"/>

Answered by Folorunso on January 22, 2021

Controller:

if ($request->hasFile('image')) {
            $image = $request->file('image');
             
            $assortment->image_path = $image->store('', 'public');
            $assortment->save();
        } 

NB: Laravel by default will create a unique name for a file and manage extension for you - dependent on your use case you may not need to make this filename yourself.


Artisan Command

php artisan storage:link

Generates the correct symlinks for your public storage.


Template

{{ Storage::url($assortment->image_path) }}

Those three things together should work.

Answered by Rory on January 22, 2021

you can use

<img src={{Storage::url($assortment->img)}} />

EDIT: I found out your problem, the problem not in the display but actually in your upload code

if ($request->hasFile('image')) {
    $image = $request->file('image'); //request the file
    $fileName = md5($image->getClientOriginalName() . microtime()) . '.' . $image->getClientOriginalExtension(); //use md5 for security reasons and get the extension.
    $image->storeAs('', $fileName, 'public'); //store the file in the public folder disk.

    $assortment->image_path = $fileName;
    $assortment->save();
} 

should become, because $filename just a temporary upload file it will be delete later, the actual upload path will be return by storeAs

if ($request->hasFile('image')) {
    $image = $request->file('image'); //request the file
    $fileName = md5($image->getClientOriginalName() . microtime()) . '.' . $image->getClientOriginalExtension(); //use md5 for security reasons and get the extension.

    $assortment->image_path = $image->storeAs('', $fileName, 'public'); //store the file in the public folder disk.
    $assortment->save();
} 

EDIT2: if you are using nginx you need to remove these line

        location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
               expires 365d;
        }

Answered by Phúc Hậu Trần on January 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP