简体   繁体   中英

how can i update multiple images in laravel

I want to update multiple images on laravel, the old images will be automatically deleted from storage after the update.

I try implementing this using the update method below:

public function update(Request $request, $id)
{

    $listing = Listing::where('id', $id)->first();
    
    $listing->title = $request->get('title');
    $listing->price = $request->get('price');
    $listing->address = $request->get('address');
    $listing->rooms = $request->get('rooms');
    $listing->city = $request->get('city');
    $listing->state = $request->get('state');
    $listing->zip_code = $request->get('zip_code');
    $listing->area = $request->get('area');
    $listing->balcony = $request->get('balcony');
    $listing->bedrooms = $request->get('bedrooms');
    $listing->bathrooms = $request->get('bathrooms');
    $listing->toilet = $request->get('toilet');
    $listing->bathroom_type = $request->get('bathroom_type');
    $listing->kitchen = $request->get('kitchen');
    $listing->parking_space = $request->get('parking_space');
    $listing->description = $request->get('description');
    $listing->featured = $request->get('featured');
    $listing->status = $request->get('status');
    $listing->type = $request->get('type');
    $listing->water_supply = $request->get('water_supply');
    $listing->power_supply = $request->get('power_supply');

    if($request->hasFile('images') != ''){    
        
        $listingImage = ListingImage::findOrFail($id);    
        //add new image
        foreach ($request->file('images') as $image) {
            $image = $request->file('images');
            $imageName = time().'.'.$image->getClientOriginalExtension();
    
            $oldImagepath = $listingImage->image_path;
            // Update the database
            $listingImage->image_path = $imageName;
            // Delete the old photo
            Storage::delete($oldImagepath);
            $listingImage->save();
            $image->move(public_path('images/listing/'.$listing->id),$imageName);
        }
    }

    $listing->save();
    return redirect('/home')->with('success', 'Listing updated!');
}

but I got the error Error Call to a member function getClientOriginalExtension() on array . My images are being stored in the ListingImage model and I have configured the default filesystem to

'local' => [
            'driver' => 'local',
            'root' => public_path('images/listing/'),
        

what steps can i take to achieve updating new images in my laravel application

in the foreach loop you are using $request->file('images') which is wrong and you are assigning it to $image variable it should be:

foreach ($request->file('images') as $image) {
         // remove this line of code
         $image = $request->file('images');
        $imageName = time().'.'.$image->getClientOriginalExtension();

        $oldImagepath = $listingImage->image_path;
        // Update the database
        $listingImage->image_path = $imageName;
        // Delete the old photo
        Storage::delete($oldImagepath);
        $listingImage->save();
        $image->move(public_path('images/listing/'.$listing->id),$imageName);
    }

First of all you change $image->getClientOriginalExtension() instead of $image->file('images)->getClientOriginalExtension() .

Second you save multiple images in column like this ["first_image.jpeg", "second_image.jpeg"]

$images = [];
foreach ($request->file('images') as $image) {
      $imageName = time().'.'.$image->getClientOriginalExtension();
      //save images in public/images/listing folder
      $image->move(public_path('images/listing/'), $imageName);
      // Delete the old photo
      $oldImagepath = $listingImage->image_path;
      Storage::delete($oldImagepath);
      $images[] = $imageName;
      $json_encode = json_encode($images);
      $listingImage->image_path = $json_encode;
}

$listingImage->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM