Laravel 中的文件上传 returns 为空

File upload returns null in Laravel

我正在努力将媒体上传添加到我的自定义 CMS。为此,我尝试了多种形式的相同上传功能。当我点击上传时,错误提示 'image' 是必填字段,即使字段中有文件。下面,我有我的模型、视图、控制器和迁移,以提供有关我如何尝试上传媒体的见解。

Media.php(型号)

class Media extends Model
{
    use HasFactory;

    protected $fillable = ['name', 'path', 'alt', 'user_id'];
}

MediaController.php(控制器)

class MediaController extends Controller
{
    public function index()
    {
        $uploads = Media::get();
        $users = User::get();
        return view('admin.media.index', compact('uploads', 'users'));
    }

    public function create()
    {
        return view('admin.media.upload');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048'
        ]);
        $name = $request->file('image')->getClientOriginalName();
        $path = $request->file('image')
             ->store('public/img/media/' . date("Y/M"));
        $upload = new Media;
        $upload->user_id = Auth::id();
        $upload->name = $name;
        $upload->path = $path;
        $upload->alt = $request->alt;
        $upload->save();

        return redirect()->route('admin.media.index')
            ->with('success', 'Media uploaded successfully');
    }
}

upload.blade.php(查看)

@extends('layouts.admin.app')
@section('title', 'Upload Media')

@section('content')
    <section class="my-4">
        <h3>{{ __('Media')}} | {{ __('Upload New Media') }}</h3>
        <a class="btn btn-warning" href="{{ route('admin.media.index') }}"> {{ __('Go Back To Media')}}</a>
    </section>

    @if ($message = Session::get('success'))
        <div class="alert alert-success">
            <p>{{ $message }}</p>
        </div>
    @endif
    <div class="flex">
        <form action="{{ route('admin.media.store') }}" enctype="multipart/form-data" method="POST" class="w-full">
            @csrf
            <div class="flex">
                <div class="w-full md:w-3/4">
                    <x-general.form.group class="">
                        <input type="file" name="file" class="form-control" id="image">
                        @error('image')
                        <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                        @enderror
                    </x-general.form.group>
                    <button type="submit" class="btn btn-dark w-full" value="{{ __('Upload Media')}}"/>
                </div>
                <div class="w-full md:w-1/4">
                    <x-general.form.group class="">
                        <img src="" id="image_preview" alt="Upload Preview" class=""/>
                        <x-admin.general.forms.label for="alt" label="{{ __('Alt Text')}}" class="small"/>
                        <input type="text" name="alt" id="alt" class="" value=""/>
                        @error('alt')
                        <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                        @enderror
                    </x-general.form.group>
                </div>
            </div>
        </form>
    </div>
@endsection

我也有截图展示过程:

错误出现是因为 <input> 元素没有 name="image。您的文件上传字段有 name="file"。变化

<input type="file" name="file" class="form-control" id="image">

<input type="file" name="image" class="form-control" id="image">