iSalam's avatar

looping through images in Laravel blade from subfolders in storage symlink ?

Hi, I am storing images uploaded by user in storage symlink. In storage folder there are multiple subfolders whose names are with id of user and in that subfolder there are images and as well as one more folder named id__revised (that as well contains images). example of directories structure:

->storage (symlink folder in public folder)
	->x(folder named with id)
		->x__revised(folder with id but revised)
			->images(multiple images in this folder)
	->image 1.png (image in x folder)
	->image2.png (another image in x folder)

so now what I am trying to achieve is that to loop through images in 'x' folder if there is image in 'x' folder it should get me images if there is 'x__revised' folder then it should return me only images in 'x__revised' folder. currently I am using these two loops

@foreach(File::glob(public_path('storage/').$orderDetails['order_number'].'/') as $path)
            <img src="{{ str_replace(public_path(), '', $path) }}" style="width: 450px; height: 450px" 				 
             class="mb-4 card border-dark"><br>
@endforeach
@foreach(File::glob(public_path('storage/').$orderDetails['order_number'].'/'.$orderDetails['order_number'].'__revised'.'/*') as $path)
           <img src="{{ str_replace(public_path(), '', $path) }}" style="width: 450px; height: 450px" class="mb-4 card border-dark">
@endforeach

which I feel is not an efficient way to achieve this thing ? any better way to get things done, your help would be highly appreciated.

0 likes
6 replies
Tray2's avatar

You should store the image paths in the database and loop over them instead of reading the filesystem.

1 like
Sinnbeck's avatar

@iSalam which part? Saving a string path to the database? Or getting them out again?

1 like
iSalam's avatar

@Sinnbeck I have saved the path instead of saving filenames as per above suggestion, currently stuck in getting them out.

Please or to participate in this conversation.