Amalmax
2635
8
Laravel

Call to a member function getClientOriginalExtension() on null

Posted 11 months ago by Amalmax

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?

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