0

I need to store images uploaded in a form, store the image in public/uploads folder and save the file name in database, and then need t show the uploaded files.

I successfully, uploaded the files to public/uploads folder but the problem it stored with a unique name generated and the name I stored in db is the original name.

....
$post_images->image_file_name = $image->getClientOriginalName();
Storage::disk('uploads')->put('prefix_'.$post_id, $image);
$post_images->save();

and while showing in view,

<img src="{{url('/uploads')}}/prefix_{{$images->post_id}}/{{$images->image_file_name}}">

the url forms for the original file name(public/uploads/some_id/my_original_file_name.png), but in public/uploads/some_id/unique_name.png has with unique name.

How do I get the unique name for the file uploaded and save it in database? Or any other way to achieve this?

Mann
  • 576
  • 1
  • 9
  • 33
  • How do you create the unique name? Provide more code please – utdev Jun 18 '17 at 13:03
  • @utdev that unique name is created by laravel. When we use `Storage::disk('uploads')->put();` it creates a unique file name and save the file. – Mann Jun 18 '17 at 13:07

1 Answers1

0

You could create another column in your database (image table) which contains the unique image path.

So after saving the original imagename with its path you could add another save function to save the unique image name (path) to your database.

Then you could loop through your blade and get the correct path.

It would look something like this then:

// get all images in your controller method and pass those to your view
public index()
{
    $images = DB::table('images') // here you have to put the table name, where your images are saved at
    return view('image.index', compact('images'))
}

In your view:

// loop through all images and get the path name from your database
@foreach($images as $imageKey => $imageValue)
     <img src="{{ $imageValue->unique_path }}" />
@endforeach

Edit:

To save the unique image name you have to do this:

$image->fill(['unique_image' => $prefix.'_'.$post_id.'.png']); // unique_image is your db column name, if its a png
$image->save();
utdev
  • 3,942
  • 8
  • 40
  • 70
  • Yea, I know I can add it ti db, how to get the unique name is my question? – Mann Jun 18 '17 at 13:08
  • Afterwards you could loop through your model and get the path name wait ill add it to my answer – utdev Jun 18 '17 at 13:10
  • I could loop and get it. I know. How do I get the unique file name first to store it db? Are you getting me? – Mann Jun 18 '17 at 13:13
  • @Mann ok I updated my answer if you have any question just ask :) hope it helps and you understand it – utdev Jun 18 '17 at 13:15
  • The code you provided, gets the files from db and shows them in view. I need how to save unique file name in database and NOT retrieving from db to show them. – Mann Jun 18 '17 at 13:19
  • Ahh ok sorry I understood it wrong give me a couple of minutes I need to think :) – utdev Jun 18 '17 at 13:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146997/discussion-between-utdev-and-mann). – utdev Jun 18 '17 at 13:20