0

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

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

我也有截图来展示这个过程: 上传表单中带有文件的表单

上传失败

4

1 回答 1

1

出现错误是因为没有<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">
于 2020-12-17T14:57:25.493 回答