shahr's avatar
Level 10

Undefined variable: data

I want to upload multiple image with dropzone but I get this error.

public function dropzone(Request $request)
{
    if ($sliders = $request->file('sliders')) {
        foreach ($sliders as $slider) {
            $randomize = rand(1111111111, 9999999999);
            $extension = $slider->extension();
            $filename = $randomize . '.' . $extension;
            $slider->move(public_path('images/design-studios/'), $filename);
            $data[] = $filename;
        }
    }
    return $slider->sliders = $data;
}

I get this error.

data

0 likes
7 replies
Snapey's avatar

Please format your code by putting 3 backticks ``` on a line before and after each code block

Snapey's avatar

You cannot add to an array that you have not yet created.

add to the top of the function

$data=[];
1 like
Sergiu17's avatar

There's an if statement, $data may be never initialized

$data = []; // initialize it as empty array
if () {
MichalOravec's avatar

You are trying to persuade php that one plus two is four.

martinbean's avatar

@oxbir Learn to read error messages. Go to line 119 of your DesignStudioController, look at what you’re doing, and actually initialise the object you’re trying to set a property on.

Please or to participate in this conversation.