FounderStartup's avatar

How to correctly upload multiple images ?

I am writing code for multiple images for the very first time :)

blade :

  <div class="p-2 border mb-4 form-group">
                                            <label class="form-label text-dark">Upload Property Images</label>
                                            <input id="demo" type="file" name="images[]" accept=".jpg, .png, image/jpeg, image/png, html, zip, css,js" multiple>
                                    </div>

Controller :

   if($request->hasfile('images')) {
            foreach($request->file('images') as $file)
            {
                $name_gen = hexdec(uniqid()) . '.' . $file->getClientOriginalExtension();
                Image::make($file)->save('upload/listings/' . $name_gen);
                $save_url = $name_gen;

                ListingImage::create([
                    'listing_image' =>  $save_url,
                    'listing_id' => 26,
                  ]);
            }

        }

'listing_id' => 26, is dummy. I actually need to save then listing id . That I will ask in my next query :)

Any help ?

0 likes
12 replies
tisuchi's avatar

@founderstartup I think you have a listing object before you start processing the photo upload. Then you can simply use $listing->id.

For example...

$listing = List::create([]);

... 
 if($request->hasfile('images')) {
            foreach($request->file('images') as $file)
            {
                $name_gen = hexdec(uniqid()) . '.' . $file->getClientOriginalExtension();
                Image::make($file)->save('upload/listings/' . $name_gen);
                $save_url = $name_gen;

                ListingImage::create([
                    'listing_image' =>  $save_url,
                    'listing_id' => $listing->id,
                  ]);
            }

        }

7 likes
tisuchi's avatar
tisuchi
Best Answer
Level 70

@FounderStartup Your code is not that readable. I also suggest you update your Laravel knowledge.

However, I just change your order a bit. Hope it will help you to solve your problem.


    $listing = Listings::create([

        'city'=> $request->city,
        'locality'=> $request->locality,
        'category'=> $request->category,
        'property_type'=> $request->property_type,
        'user_id' => Auth::id(),
        'bhk' => $request->bhk,
        'size' =>$request->size,
        'unit' => $request->unit,
        'furnishing' => $request->furnishing,
        'listingtype' => $request->listingtype,
        'landarea' => $request->landarea,
        'landareaunit' => $request->landareaunit,
        'hotelrooms' => $request->hotelrooms,
        'free' => $free,
        'latitude' => $request->latitude,
        'longitude' => $request->longitude,
        'autocomplete' => $request->autocomplete,
        'amount' => $request->amount,
        'toilets' => $request->toilets,
        'parkings' => $request->parkings,
        'packid' =>  $bulklistings->id,
        'description' =>  $request->description,
        'slug' => strtolower(str_replace(' ', '-',$slug)),
        'status' => 1,
        'created_at' => Carbon::now(),
        'expiry_date'=> Carbon::now()->addDays($days),
    ]);


    if($request->hasfile('images')) {
        foreach($request->file('images') as $file)
        {
            $name_gen = hexdec(uniqid()) . '.' . $file->getClientOriginalExtension();
            Image::make($file)->save('upload/listings/' . $name_gen);
            $save_url = $name_gen;

            ListingImage::create([
                'listing_image' =>  $save_url,
                'listing_id' => $listing->id
              ]);
        }
    }

6 likes
FounderStartup's avatar

@MichalOravec

Kindly don't spoil this lovely platform with your negative comments. If you can't help at least don't post such comments. Sorry but such comments are not welcome.

thinkverse's avatar

@FounderStartup Have you tried to read the documentation? There's a full section on File Uploads, including best practices like not using getClientOriginalName() or getClientOriginalExtension(), since those are considered unsafe. And about storing the uploaded files using either store or storeAs.

1 like
MichalOravec's avatar

@FounderStartup I'm just warning you that your code isn't ready for use in the real world and in addition you have no idea what you're doing.

FounderStartup's avatar

@MichalOravec

Kindly stay away. Good for both of us. My suggestion is that start respecting people and they will respect you also. Why would somebody visit this site if he/she knows everything of laravel ? So thanks for your negative comments and kindly stay aways from my post.

Please or to participate in this conversation.