Skip to content
Advertisement

Laravel 8: Undefined offset error message

I’m working with Laravel Controllers:

class AdminController extends Controller
{
    protected function uploadImages($file)
    {
        $year = Carbon::now()->year;
        $imagePath = "/upload/images/{$year}/";
        $filename = $file->getClientOriginalName();
        $file = $file->move(public_path($imagePath), $filename);
        $sizes = ["300","600","900"];
        $url['images'] = $this->resize($file->getRealPath(), $sizes, $imagePath, $filename);
        $url['thumb'] = $url['images'][$sizes[0]];
        dd($url);
    }
    private function resize($path, $sizes, $imagePath, $filename)
    {
        $images['original'] = $imagePath . $filename;
        foreach($sizes as $size)
        {
            $image[$size] = $imagePath . "{$size}" . $filename;
            Image::make($path)->resize($size, null, function($constraint){
                $constraint->aspectRatio();
            })->save(public_path($image[$size]));
        }
        return $images;
    }
}

When I fill the form that comes with this Controller I get this message:

ErrorException Undefined offset: 300

From this line:

$url['thumb'] = $url['images'][$sizes[0]];

So what is the problem here?

Advertisement

Answer

The problem is probably this line:

$image[$size] = $imagePath . "{$size}" . $filename;

it should be:

$images[$size] = $imagePath . "{$size}" . $filename;

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement