未在编辑表单中选择图像文件时,如何使用当前图像更新配置文件?我正在使用Laravel 8 [英] How to update profile with current image when an image file is not chosen in the edit form? I am using Laravel 8

查看:57
本文介绍了未在编辑表单中选择图像文件时,如何使用当前图像更新配置文件?我正在使用Laravel 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试仅将个人档案更改保存为个人档案描述和url到编辑表单中时,都会出现错误,因为我也没有选择图像文件.

Whenever I try to only save profile changes for the profile description and url in the edit form, I get an error because I didn't choose an image file also.

当我未在编辑表单中选择图像文件时,我希望能够使用当前图像更新配置文件.

I would like to be able to update a profile with current image when an image file is not chosen in the edit form.

我一直收到的错误是:在null上调用成员函数store()

The error I keep getting is: Call to a member function store() on null

...该错误是在我的UserController的update方法中引用此行:

...that error is referring to this line in the update method of my UserController:

$imagePath = request('image')->store('uploads', 'public');

这是我的UserController中的整个更新方法:

This is the entire update method in my UserController:

public function update(User $user, Request $request)
     {

       $data = request()->validate([
         'description' => 'nullable',
         'url' => 'nullable',
         'image' => 'nullable',
       ]);

       $imagePath = request('image')->store('uploads', 'public');

       auth()->user()->profile()->update([
         'description' => $data['description'],
         'url' => $data['url'],
         'image' => $imagePath,
       ]);

    return redirect('/users/' . auth()->user()->id);

     }

最后,这是我的edit-profile.blade.php文件中的表格:

Finally, this is the form in my edit-profile.blade.php file:

@section('content')
<body class="home_body">
  <div class="home_container_div">
      <div class="home_container">
          <div class="home_box_div">

            <form action="{{('/users/' . auth()->user()->id)}}" enctype="multipart/form-data" method="post">
              @csrf
              @method('PATCH')

              <div class="form-group">
                  <label for="description" class="edit_description_label">Description</label>

                  <div class="edit_description_div">
                      <input id="description"
                      type="text"
                      class="form-control @error('description') is-invalid @enderror"
                      name="description"
                      value="{{ old('description' ) ?? auth()->user()->profile->description }}"
                      autocomplete="description" autofocus>

                      @error('description')
                      <div class="invalid-feedback-div">
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                      </div>
                      @enderror
                  </div>
              </div>

              <div class="form-group">
                  <label for="url" class="edit_title_label">URL</label>

                  <div class="edit_url_div">
                      <input id="url"
                      type="text"
                      class="form-control @error('url') is-invalid @enderror"
                      name="url"
                      value="{{ old('url' ) ?? auth()->user()->profile->url }}"
                      autocomplete="url" autofocus>

                      @error('url')
                      <div class="invalid-feedback-div">
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                      </div>
                      @enderror
                  </div>
              </div>

              <div class="create_post_image_div">
                <label for="image" class="create_image_label">Profile Image</label>
                <input type="file" class="form-control-file" id="image" name="image">

                @error('image')
                <div class="invalid-feedback-div">
                  <strong>{{ $message }}</strong>
                </div>
                @enderror

                <div class="create_post_btn_div">
                  <button class="create_post_btn">Save Profile</button>
                </div>
              </div>

            </form>

          </div>
      </div>
  </div>
</body>
@endsection

如何解决此问题?

推荐答案

如果用户未选择任何图像文件,则可以跳过图像上传部分,例如:

You can skip the image upload part if the user has not selected any image file, like this:

public function update(User $user, Request $request)
{

    $data = request()->validate([
        'description' => 'required',
        'url' => 'required',
        'image' => 'nullable',
    ]);

    $updateData = [
        'description' => $data['description'],
        'url' => $data['url'],
    ];

    if (request('image')) {
        $imagePath = request('image')->store('uploads', 'public');
        $updateData['image'] = $imagePath;
    }

    auth()->user()->profile()->update($updateData);

    return redirect('/users/' . auth()->user()->id);
}

这篇关于未在编辑表单中选择图像文件时,如何使用当前图像更新配置文件?我正在使用Laravel 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆