Msoft's avatar
Level 1

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

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','VehicleController@store');

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?

0 likes
11 replies
abhishektrivedi's avatar

first check this line is it returning image data?

$photos = $request->file('file');
rivory_g's avatar

Did you had encryption type on your form ?

    <form method="POST" action="[route]" class="dropzone" enctype="multipart/form-data">
Msoft's avatar
Level 1

@rivory_g I am using div to dropzone box like this,

 <div class="dropzone" id="my-dropzone"> 
    <div class="dz-message">
    
              <div class="col-xs-8">
                 <div class="message">
                    <p>Drop files here or Click to Upload</p>
                 </div>
              </div>
    </div>
    <div class="fallback">
        <input type="file" name="file" multiple>
        
    </div>
</div>
Msoft's avatar
Level 1

@hdsavani @rivory_g see my complete blade file

 <form method="post" action="{{url('form')}}" enctype="multipart/form-data"> 
            {{csrf_field()}}
            <input type="hidden" id="cid" name="cid" value="{{ $catagories->id }}" />
        <div class="form-group">
        <label for="exampleFormControlSelect1">District</label>
        <select class="form-control" id="exampleFormControlSelect1" name="district">
        <option>Colorado</option>
        <option>Paris</option>
        <option>New York</option>
        <option>Amgs</option>
        <option>jeffery</option>
        </select>
        </div>

        <div class="form-group">
        <label for="exampleFormControlSelect1">Town</label>
        <select class="form-control" id="exampleFormControlSelect1" name="town">
        <option>point snt</option>
        <option>akala ertyy</option>
        <option>port canel</option>
        <option>walu</option>
        <option>sudan</option>
        </select>
        </div>
            
        <div class="form-group">
        <label for="exampleFormControlSelect1">Brand</label>
        <select class="form-control" id="exampleFormControlSelect1" name="brand">
        <option>Toyota</option>
        <option>Nissan</option>
        <option>Mitsubishi</option>
        <option>BMW</option>
        <option>Benz</option>
        </select>
        </div>

        <div class="form-group">
        <label for="exampleFormControlSelect1">Model</label>
        <select class="form-control" id="exampleFormControlSelect1" name="model">
        <option>121</option>
        <option>aqua</option>
        <option>yaris</option>
        <option>primio</option>
        <option>codak</option>
        </select>
        </div>
 <!--  start dropzone image upload-->
       <div class="dropzone" id="my-dropzone"> 
    <div class="dz-message">
    
              <div class="col-xs-8">
                 <div class="message">
                    <p>Drop files here or Click to Upload</p>
                 </div>
              </div>
    </div>
    <div class="fallback">
        <input type="file" name="file" multiple>
        
    </div>
</div>
<!--end dropzone image upload-->

        <button type="submit" class="btn btn-primary" style="margin-top:10px">Submit</button>



        </form>

Msoft's avatar
Level 1

abhishektrivedi yes it is null

Msoft's avatar
Level 1

how to fix my null images in

$photos = $request->file('file');
rivory_g's avatar

Well there is different misbehaviour in the way you try to handling it.

From what I understand, dropzone and your view' form are different entities.

Right now I would say that when you add images to dropzone, it is dynamically trying to send your images one by one through an ajax request to your route. If you want to pursue this way you need to append the csrf token to this ajax request. But having it within a form lead to a misconception to me, cause let say you have 10 images to upload, you are going to submit the form (with the select fields) before that all the images are being sent to your server one by one .

So If I were you I would try to follow the link sent before explaining how to combine a normal form and dropzone, which is basicaly explaining that the images are going to be sent only after the submit button' click in your request. You could then treat the files send as an array. (request->file[])

Please or to participate in this conversation.