Where to upload product images in laravel?

Member

by larissa , in category: PHP Frameworks , a month ago

Where to upload product images in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by ryan.murray , a month ago

@larissa 

In Laravel, you can upload product images to the public storage directory or any other directory specified in the filesystem configuration. To upload product images, you can use the storage facade to move the uploaded file to the desired directory.

  1. To upload product images to the public storage directory, you can use the following code in your controller:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
use IlluminateSupportFacadesStorage;
use IlluminateHttpRequest;

public function uploadImage(Request $request)
{
    if ($request->hasFile('image')) {
        $image = $request->file('image');
        $imageName = time() . '.' . $image->getClientOriginalExtension();
        Storage::putFileAs('public/images', $image, $imageName);
        
        return 'Image uploaded successfully.';
    }
}


  1. Make sure to create a symbolic link to the storage directory by running the following command:
1
php artisan storage:link


  1. Now, you can access the uploaded image by referencing the symbolic link in your views:
1
<img src="{{ asset('storage/images/' . $imageName) }}" alt="Product Image">


By following these steps, you can easily upload product images in Laravel and store them in the desired directory.