abhishekganguly21@gmail.com's avatar

Store uploaded image path to database.

I am able to upload a image file using store () method to storage/images folder but how to store image path with the file name to the database?

0 likes
1 reply
Screenbeetle's avatar
Level 15

Good Morning

I think your images should be at this path storage/app/public

If you run php artisan storage:link it should create this directory and add a sym link to your public directory. Please read more here - https://laravel.com/docs/5.4/filesystem#the-public-disk

Then from Laravel 5.3 you can do the following in your store method:

$model = Model::create($request->all());

if ($request->hasFile('image')) {
    $request->file('image')->store('public/images');
    
    // ensure every image has a different name
    $file_name = $request->file('image')->hashName();
    
    // save new image $file_name to database
    $model->update(['image' => $file_name]);
}

// then in your view you reference the path like this:
 
<img src="{{ asset('public/images/' . $model->image) }}">

4 likes

Please or to participate in this conversation.