简体   繁体   English

当编辑表单中未选择图像文件时,如何使用当前图像更新配置文件? 我正在使用 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

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.每当我尝试在编辑表单中仅保存配置文件描述和 url 的配置文件更改时,我都会收到错误消息,因为我也没有选择图像文件。

I would like to be able to update a profile with current image when an image file is not chosen in the edit form.当未在编辑表单中选择图像文件时,我希望能够使用当前图像更新配置文件。

The error I keep getting is: Call to a member function store() on null我不断收到的错误是:调用 null 上的成员 function store()

...that error is referring to this line in the update method of my UserController: ...该错误是指我的 UserController 的更新方法中的这一行:

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

This is the entire update method in my UserController:这是我的 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);

     }

Finally, this is the form in my edit-profile.blade.php file:最后,这是我的 edit-profile.blade.php 文件中的表格:

@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

How can I resolve this issue?我该如何解决这个问题?

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当尚未在编辑表单中选择图像时,如何默认使用当前图像更新帖子。 我正在使用 laravel 8 - How to by default update a post with the current image when an image has not been chosen in edit form. I am using laravel 8 如何使用laravel 6在表单编辑中获取图像以进行更新 - how to get image in form edit for update using laravel 6 加载当前图像以在编辑表单中更新(在Laravel 5.5中使用表单绑定) - Load Current Image to Update in Edit Form (Using Form Binding in Laravel 5.5) 我使用 laravel 6 看不到我的更新个人资料图片 - I can't see my update profile image using laravel 6 从编辑表单Laravel更新图像 - Update image from edit form Laravel 如何绑定当前图像以使用Laravel更新表单字段? - How to bind current image to update form field with Laravel? 如何在 laravel 的编辑视图中更新图像? - How can I update image in edit view in laravel? 在laravel 5.8 中更新图像时文件输入表单的默认值 - default value for file input form when update image in laravel 5.8 在 Laravel 中设置编辑图像时需要的文件? - Set file required when edit image In Laravel? Laravel:如何同时编辑用户信息和上传头像? - Laravel: How can I edit user information and upload profile image at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM