Msoft
1 year ago
455
11
Laravel

how to fix Call to a member function getClientOriginalExtension() on null in laravel 5.6

Posted 1 year ago by Msoft

in my laravel application I am using dropzone programmatically to upload images. this is my Controller to store images in VehicleController

public function store(Request $request)
    {


    $photos = $request->file('file');

        if (!is_array($photos)) {
            $photos = [$photos];
        }

        if (!is_dir($this->photos_path)) {
            mkdir($this->photos_path, 0777);
        }

        for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];
            $name = sha1(date('YmdHis') . str_random(30));
            $save_name = $name . '.' . $photo->getClientOriginalExtension();//this is line 75
            $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();

            Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);

            $photo->move($this->photos_path, $save_name);

            $upload = new Upload();
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->save();
        }
        return Response::json([
            'message' => 'Image saved Successfully'
        ], 200);

and programatically jquery is

Dropzone.autoDiscover = false;

// Dropzone class:
var myDropzone = new Dropzone("div#my-dropzone", { url: '/form'});

routes

Route::post('form','[email protected]');

but when my form submit button clicked following errors occurred,

1/1) FatalErrorException

Call to a member function getClientOriginalExtension() on null
in VehicleController.php line 75

how to fix this problem?

Please sign in or create an account to participate in this conversation.