How to update image where is null?

  laravel, php

I am currently working on a project with an update function using laravel the update function is working fine, but my problem is that when i try to update a record and stay it as is and when i see the database the image column is empty..how can i check if the file is empty and when its empty do not update?because when i try to update a record with an image and hit the update button the image is gone..

public function update(Request $request)
{
    $user = auth()->user();
    if ($user->image == null) {
        $path = null;            
    } else {
        if ($request->hasFile('image')) {
            $file = $request->file('image');
            $nane = time();
            $extension = $file->getClientOriginalExtension();
            $fileName = $nane . '.' . $extension;
            $path = $file->storeAs('images/users', $fileName, 'public');
        }
    }
    $user->image = $path;
    $user->first_name = $request->first_name;
    $user->last_name = $request->last_name;
    $user->job_title = $request->job_title;
    ......
    $user->save();
}

Source: Laravel

Leave a Reply