Shawdow's avatar

How to Store Multiple images filename in one column Using Laravel

Hi,

I have tried with upload multiple images to the database it gets uploaded to the images folder successfully in the database it shows only one images folder name is this possible in the laravel to store two images filename in one column

in the uploadcontroller.php file


$prod = new \App\Product;
      
       $prod->name =  Input::get('name');

  if(! is_null(request('images')))
        {
            $photos=request()->file('images');
            
            $uploadcount = 0;
            
            foreach ($photos as $photo)
             {
                $destinationPath = 'images';
                $filename =  $photo->getClientOriginalName();
                $photo->move($destinationPath,$filename);
                $uploadcount ++;
               
                $photo->getClientOriginalExtension();
                $prod->mime = $photo->getClientMimeType();
                $prod->filename =  $filename;
                       
          }
        
    }
       $prod->save();

Thanks,

0 likes
9 replies
zainudinnoori's avatar

That is not a good practice to add tow file name in a single column. beacause while retrieving those images it will get complicated. instead you can add another column to your table and specify that in to nullable(). and then store the second image or file into your table.

Shawdow's avatar

@zainudinnoori okay Sir Suppose if it as three images i need to create another column that column should be null sir

zainudinnoori's avatar

@Shawdow I didn't get what exactly you want to want to do. You are storing each image in a different row in your table. and now why you want to add two file name in single row.

Shawdow's avatar

@zainudinnoori i need add the Product name and product image to the database like an ecommerce site. I will take the input of Product name and Product images from the user and store it in the database.Suppose if the user has two product images i need to store two product images to the database this what i wanted to do

satiseven777's avatar
Level 4

you should create a other table that can be named images and make a foreign key between product table and images table,you are using very old method for storing your images link :~) you should use one to many relation between your product and images table! you can find very helpful docs in laravel own site! if you had and problems I can send you sample code!

3 likes
Rachid804's avatar

Like @zainudinnoori said it'l not a good practice to store images this way, but if you want to go this way and use only 1 column you can: store images name as strings separated by a distinguished character like ; | , for exemple:

$images = "image1.jpg;image2.jpg;image2.png";

//After retrieving the column you can use it like this:
$images = explode(";", $images);

//Result
[                
   "image1.jpg",  
   "image2.jpg",  
   "image2.png",  
]          

The best practice is to store images in separate table, and create a relationship between the Product model and Image model, the great news for this option is there is a great packages to do this out of the box:

Media Library from spatie (My favorite)

Intervention

Laravel stapler

Shawdow's avatar

I think i need to go with the separate table to store the images in the database make the relationship b/w the products and images according to above laravelers thank you :)

Shawdow's avatar

@satiseven777 sir first I will try with my code if i had any problem try to post in laracast get the code works thank you :)

bunnypro's avatar

just for an alternative if you still want a single column to store your images you can make a json column. the simplest way is make a textcolumn and mark the column in $casts property of your model as array, it will serialize to database as a string by json_encode it, or create attribute accessor and mutator to implement your own serialize/unserialize logic.

Please or to participate in this conversation.