0

当我尝试编辑没有图片的用户帖子时,我收到此错误“调用 null 时的成员函数 store()”,但是当我尝试编辑带图片的用户帖子时,我没有收到错误。

这是我的html代码

<div class="col-lg-4 col-md-4 col-sm-4">
    <div class="ur-thumb">
        <label for="picture">
            <input id="picture" type="file" name="cv_picture" style="display: none" onchange="readUrl(this)">
            <img id="myImage" src="{{Storage::url($usersCv->picture)}}" class="img-responsive pointer" alt="" />
        </label>

    </div>
    @error('cv_picture')
    <strong style="color: red">{{$message}}</strong>
    @enderror
</div>

这是我的控制器

  public function updateUserCv(CvRequest $request, $id)
{
    $userId = Auth::id();
    Cv::where('id', $id)
        ->where('user_id', $userId)
        ->update([
            'name' => $request['name'],
            'surname' => $request['surname'],
            'father_name' => $request['fatherName'],
            'gender' => $request['gender'],
            'year' => $request['year'],
            'category_id' => $request['catId'],
            'position_name' => $request['position'],
            'type_id' => $request['typId'],
            'education' => $request['education'],
            'education_description' => $request['about_education'],
            'experience_id' => $request['expId'],
            'experience_descriptions' => $request['about_work'],
            'city_id' => $request['cityId'],
            'salary' => $request['salary'],
            'skills' => $request['skills'],
            'other_information' => $request['other'],
            'mobile' => $request['mobile'],
            'email' => $request['email'],
            'picture' => ($request->file('cv_picture')->store('public/profile')),

        ]);
    toastr()->success('Success.', 'ok!');
    return redirect('user-cv/'.$id);

}
4

1 回答 1

0

你有一个表达式$request->file('cv_picture'),它返回null,然后你尝试store在它上面调用方法。

要修复它,请添加对返回值的检查 - 类似于

'picture' => $request->file('cv_picture') ? $request->file('cv_picture')->store('public/profile') : null

更新:好的,我错过了编辑记录时可能无法重新上传的图像。因此,您需要单独为update()方法准备数据,并且仅在图片属性存在时才设置它。像这样的东西:

public function updateUserCv(CvRequest $request, $id)
{
    $update_data = [
        'name' => $request['name'],
        'surname' => $request['surname'],
        'father_name' => $request['fatherName'],
        'gender' => $request['gender'],
        'year' => $request['year'],
        'category_id' => $request['catId'],
        'position_name' => $request['position'],
        'type_id' => $request['typId'],
        'education' => $request['education'],
        'education_description' => $request['about_education'],
        'experience_id' => $request['expId'],
        'experience_descriptions' => $request['about_work'],
        'city_id' => $request['cityId'],
        'salary' => $request['salary'],
        'skills' => $request['skills'],
        'other_information' => $request['other'],
        'mobile' => $request['mobile'],
        'email' => $request['email'],
    ];
    $file = $request->file('cv_picture');
    if ($file) {
        $update_data['picture'] = $file->store('public/profile');
    }
    $userId = Auth::id();
    Cv::where('id', $id)
        ->where('user_id', $userId)
        ->update($update_data);
    toastr()->success('Success.', 'ok!');
    return redirect('user-cv/'.$id);

}
于 2020-03-15T22:45:57.330 回答