Amalmax's avatar

Call to a member function getClientOriginalExtension() on null

hello, I am working with laravel 5.6 and going to update existing image on uploads table in my edit.blade.php form, my edit view is as following,

 @if( $vehicles->uploads->count() > 0 )
                        
                            @php
                                $upload = $vehicles->uploads->sortByDesc('id')->first();
                            @endphp
                         
                            <img id="preview"
                         src="{{asset((isset($upload) && $upload->resized_name!='')?'images/'.$upload->resized_name:'images/noimage.png')}}"
                         height="200px" width="200px"/>
                    <input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="{{ csrf_token() }}" enctype="multipart/form-data">
                    <br/>
                    <a href="javascript:changeProfile();">Add Image</a> |
                 

               <a class="button is-outlined" href="/myads/{{$upload->id}}/delete" onclick="return confirm('Are you sure to want to delete this record?')" >Delete</a></td>
                   <hr>
                    @endif

and my Controller update function is,

 public function update(Request $request, $id)
    {
 $photos = $request->file('files');
 
        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 198
            $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 = Upload::find($id);
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->vehicle_id = $vehicle->id;
            $upload->save();
        }

and My Upload table structure is as following,

id | filename | resized_name | original_name | vehicle_id

but when I click update button this error msg occured

1/1) FatalErrorException

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

how can fix this problem?

0 likes
8 replies
tykus's avatar

Make sure that your form has `enctype=multipart/form-data" attribute

1 like
biishmar's avatar
biishmar
Best Answer
Level 11

@Amalmax You have declared the enctype in an input field, I think it should be in a form field.

1 like
NOMGUY's avatar

After this,

for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];

put,

<?php
    echo "<pre>";
    print_r($photo);
    die();
?>
OBp's avatar

in your form:open you need the 'files' => true like below

Form::open('your_path', array('files'=> true))

or

// Must be used multipart/form-data for image uploading.

Amalmax's avatar

My route is also here,

Route::get('myads/{id}/edit', [
    'uses' => '\App\Http\Controllers\VehicleController@edit',
    'as'=> 'vehicles.edit'
]);

Route::post('myads/{id}', [
    'uses' => '\App\Http\Controllers\VehicleController@update',
])->name('vehicles.edit');

and my controller edit function

 public function edit($id)
    {
        $vehicles = Vehicle::findOrFail($id);
return view('vehicles.edit')->withVehicles;
}

but when I click edit button error came with this url:

http://localhost:8000/myads/16 <- this is vehicle_id
tykus's avatar

@tykus yes I have it you can see it on my form...

Are you joking???

Please or to participate in this conversation.