上传图片时返回 null Laravel 7

Returning null when i upload image Laravel 7

我想上传我的照片,但是当我用 dd() 检查时它会 return 空值。

dd($request->file('profile_image'));

但是当我检查

dd($request->all());

我的控制器

public function update(Request $request)
    {
        $time_date = \Carbon\Carbon::now()->toDateTimeString();
        $user = Auth::user();
        dd($request->all());
        dd($request->file('profile_image'));
        if($user->role_id =='1'){
            if($request->hasFile('profile_image')){
                $file = $request->file('profile_image');
                $extension = $file->getClientOriginalExtension();
                $filename = $request->id.'.'.$extension;
                $file->move(public_path($file->getClientOriginalName()),public_path('user/'.$filename));
                $file->save();
            }
            $id = $request->input('id');
            $data_user = User::find($id);
            $data_user->id = $request->input('id');
            $data_user->nama_lengkap = $request->input('name');
            $data_user->email = $request->input('email');
            $data_user->password = $request->input('password');
            $data_user->gender = $request->input('gender');
            $data_user->last_modified_by = Auth::user()->nama_lengkap;
            $data_user->last_modified_time = $time_date;
            $data_user->save();

            return redirect('/indexadmin')->with('Berhasil Edit','Berhasil Edit');
        }
        elseif($user->role_id =='2'){
            return redirect('/indexuser');
        }
        
    }

我的edit.blade.php

<form class="border-bottom" method="POST" action="{{ url('/update-user/') }}" enctype='multipart/form-data'>
            @method('PATCH')
            @csrf
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                        <span class="fas fa-exclamation fa-fw"></span>
                    </span>
                </div>
                <input type="number" class="form-control @error('id') is-invalid @enderror" value="{{ $data_user->id }}" placeholder="Id" aria-label="Id" name="id" aria-describedby="basic-addon1">
                @error('id')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
                @enderror
            </div>
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                        <span class="fas fa-user fa-fw"></span>
                    </span>
                </div>
                <input type="text" class="form-control @error('name') is-invalid @enderror" value="{{ $data_user->nama_lengkap }}" placeholder="Name" aria-label="Name" name="name" aria-describedby="basic-addon1">
                @error('name')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
                @enderror
            </div>
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                        <span class="fas fa-envelope fa-fw"></span>
                    </span>
                </div>
                <input type="email" class="form-control @error('email') is-invalid @enderror" value="{{ $data_user->email }}" placeholder="Email" aria-label="Email" name="email" aria-describedby="basic-addon1">
                @error('email')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
                @enderror
            </div>
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                        <span class="fas fa-key fa-fw"></span>
                    </span>
                </div>
                <input type="password" class="form-control @error('password') is-invalid @enderror" value="{{ $data_user->password }}" placeholder="Password" aria-label="Password" name="password" aria-describedby="basic-addon1">
                @error('password')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
                @enderror
            </div>
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                        <span class="fas fa-mars-double fa-fw"></span>
                    </span>
                </div>
                <select class="form-control" name="gender">
                    <option value="{{ $data_user->gender }}">Laki-Laki</option>
                    <option value="{{ $data_user->gender }}">Perempuan</option>
                </select>
            </div>
            <div class="input-group mb-3">
                <input type="file" name="profil_image" id="profil_image" class="form-control">
            </div>
            <button type="adding" class="btn btn-outline-success btn-lg btn-block mt-3 mb-3">
                <div class="row">
                    <div class="col-2 text-left">
                        <span class="fas fa-arrow-circle-up">
                    </div>
                    <div class="col-8 text-center">
                        Save
                    </div>
                </div>
            </button>
        </form>
        <form action="/">
        <button type="adding" class="btn btn-outline-danger btn-lg btn-block mt-3 mb-3">
                <div class="row">
                    <div class="col-2 text-left">
                        <span class="fas fa-arrow-circle-down">
                    </div>
                    <div class="col-8 text-center">
                        Cancel
                    </div>
                </div>
        </button>
        </form>

有什么解决办法吗?

*文件未移动到 public/user 或 storage/public。

试试这个

$file->move(public_path(public_path('user/'.$filename),$file->getClientOriginalName()));

有拼写错误请更正

因为您的 dd($request->all()) 显示您的变量名称是 profil_image

改变

dd($request->file('profile_image');

dd($request->file('profil_image');

并且在文件保存代码也更改如下

 if($request->hasFile('profil_image')){
            $file = $request->file('profil_image');
            $extension = $file->getClientOriginalExtension();
            $filename = $request->id.'.'.$extension;
            $file->move(public_path($file->getClientOriginalName()),public_path('user/'.$filename));
            $file->save();
        }