封面图像必须是一个图像在laravel 8

dkqlctbz  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(124)

我是Laravel 8的新手,我一直在尝试上传一个带有文件字段的表单,但我一直收到一个错误“图像必须是图像”,即使我在表单中包含了enctype='multipart/form-data'

刀片模板

@extends('layouts.app')
@section('contents')
    <div class="container">
        <h1> Create a Posts</h1>
        {!! Form::open(['action' =>['App\Http\Controllers\PostController@store','enctype'=>'multipart/form-data', 'method'=>'POST']]) !!}
        <div class="form-group">
            {{Form::label('title', 'Title')}}
            {{Form::text('title','',['class'=>'form-control','placeholder'=>'Title'])}}
        </div>
        <div class="form-group">
            {{Form::label('body', 'Body')}}
            {{Form::textarea('body','',['class'=>'form-control','id'=>'article-ckeditor','placeholder'=>'Bodytext'])}}
        </div>
        <br>
        <div class="form-group">
            <div class="image">
                {{Form::file('cover_image')}}
            </div>
        </div>
        <br>
        {{Form::submit('Submit',['class'=>'btn btn-primary']);}}
        {!! Form::close() !!}
    </div>
@endsection

主计长

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\User;
public function store(Request $request)
{
    $validatedData = $request->validate([
        'title' => ['required', 'unique:posts', 'max:500000'],
        'body' => ['required'],
        'cover_image'=>['image','nullable','max:19999']
    ]);
    //handle file uploads
    if($request->hasfile('cover_image')){
        return '123';
    }
}

路线

Route::resource('/post', PostController::class);
kzipqqlq

kzipqqlq1#

您需要在打开表单时允许文件上载:

@extends('layouts.app')
@section('contents')
    <div class="container">
        <h1> Create a Posts</h1>
        {!! Form::open(['action' =>['App\Http\Controllers\PostController@store','enctype'=>'multipart/form-data', 'method'=>'POST', 
// add files true in the form open
'files' => true]]) !!}
        <div class="form-group">
            {{Form::label('title', 'Title')}}
            {{Form::text('title','',['class'=>'form-control','placeholder'=>'Title'])}}
        </div>
        <div class="form-group">
            {{Form::label('body', 'Body')}}
            {{Form::textarea('body','',['class'=>'form-control','id'=>'article-ckeditor','placeholder'=>'Bodytext'])}}
        </div>
        <br>
        <div class="form-group">
            <div class="image">
                {{Form::file('cover_image')}}
            </div>
        </div>
        <br>
        {{Form::submit('Submit',['class'=>'btn btn-primary']);}}
        {!! Form::close() !!}
    </div>
@endsection

see https://laravelcollective.com/docs/6.x/html#opening-a-form

相关问题