vickypandey's avatar

Undefined array key "image" while updating data

Hello, I have a problem with my laravel project (I'm quite new to Laravel). So I'm creating an e-commerce website and everything was fine until i decided to update my slider data and image to my slider. Now when I am submitting my form i have an error: Undefined array key "image".

public function update(SliderFormRequest $request, Slider $slider) { $validatedData = $request->validated();

    if($request->hasFile('image')){

        $destination = $slider->image;
        if(File::exists($destination)){
            File::delete($destination);
        }

        $file = $request->file('image');
        $ext = $file->getClientOriginalExtension();
        $filename = time().'.'.$ext;
        $file->move('uploads/slider/', $filename);
        $validatedData['image'] = "uploads/slider/$filename";    
    }

    $validatedData['status'] = $request->status == true ? '1':'0';
    
    Slider::where('id',$slider->id)->update([
        'title' => $validatedData['title'],
        'description' => $validatedData['description'],
        'image' => $validatedData['image'],
        'status' => $validatedData['status'],
    ]);

    return redirect('admin/sliders')->with('message', 'Slider Added Successfully!');
}

it show error while updating image 'image' => $validatedData['image'],

0 likes
4 replies
MichalOravec's avatar

Again and again..

Explain me the code below:

Slider::where('id',$slider->id)

You defined $validatedData['image'] inside the if, so what do you think will happen if you don't upload the image?

vickypandey's avatar

no this error is coming only when i am updating the image. And if I update the rest of the slider data without updating the image, then I am not getting this error. And even if I remove this 'image' => $validatedData['image'],, the rest of the data is getting updated except the image.

kodyxgen's avatar

You can dd($validatedData); before the Slider::where ... and inspect the array that you are getting.

P.S. you don't need to make an extra query to get the slider, you already have acces to it just $slider->update($validatedData)

1 like
vickypandey's avatar

I forgot to keep the image null in the migration of this slider. And then I did some changes in the slider controller so that it got fixed

public function update(SliderFormRequest $request, Slider $slider) { $validatedData = $request->validated();

    if($request->hasFile('image')){

        $destination = $slider->image;
        if(File::exists($destination)){
            File::delete($destination);
        }

        $file = $request->file('image');
        $ext = $file->getClientOriginalExtension();
        $filename = time().'.'.$ext;
        $file->move('uploads/slider/', $filename);
        $validatedData['image'] = "uploads/slider/$filename";    
    }

    $validatedData['status'] = $request->status == true ? '1':'0';
    
    Slider::where('id',$slider->id)->update([
        'title' => $validatedData['title'],
        'description' => $validatedData['description'],
        'image' => $validatedData['image'] ?? $slider->image,
        'status' => $validatedData['status'],
    ]);

    return redirect('admin/sliders')->with('message', 'Slider Updated Successfully!');
}

I put the condition for the image here. thank you for helping me......

1 like

Please or to participate in this conversation.