mysql 图片必须是图片,Laravel验证错误

sxpgvts3  于 2023-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(120)

我尝试在表单注册表上上传图像,但当我提交此表单时,收到此错误“图像必须是图像”。我已经更改了模型以接受所有图像扩展名,并尝试在表单上添加“enctype=“multipart/form-data”“,但它不起作用。
我该如何解决这个问题?我需要上传这张照片。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Livros;

class Livro extends Model
{
    use HasFactory;
    
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'livro',
        'users_id',
        'namel',
        'autor',
        'editora',
        'categoria',
        'classificação',
        'descricao',
        'image'=>'required|image|mimes:jpeg,jpg,png,bmp,gif,svg',

    ];

}

控制器

protected function validator(Request $request)
{
        return Validator::make($request, [
            'namel' => ['required', 'string', 'max:200'],
            'autor' => ['required', 'string', 'email', 'max:200'],
            'editora' => ['required', 'string', 'max:50'],
            'categoria'=> ['required', 'string', 'min:50'],
            'classificação'=> ['required', 'string', 'min:1','max:2'],
            'descricao'=> ['required', 'string', 'min:200'],
            'image'=> ['required'],
  
        ]);
    }

     /**
      * Store a newly created resource in storage.
      *
      * @param  \Illuminate\Http\Request  $request
      * @return \Illuminate\Http\Response
      */
    public function create(StoreUpdateLivro $request)
    {
        
        $user = Auth::user()->id;
        $data = $request->all();

        if($request->file('image')->isValid()){

           $image = $request->image->store('livros');
           $data['image'] = $image;
        }

        
        Livro::create([
            'users_id' => $user,
            'namel' => $request['namel'],
            'autor' => $request['autor'],
            'editora' => $request['editora'],
            'categoria'=> $request['categoria'],
            'classificação'=>$request['classificação'] ,
            'descricao'=>$request['descricao'],
            'image'=>$request['image'],
        ]);
        return view('livros/cadastro');

        
    }

刀锋

<form method="POST" enctype=”multipart/form-data” action="{{ url('/cadastro_livros') }}">
       <div class="form-group row">
                                <label for="image" class="col-md-4 col-form-label text-md-right">{{ __('CAPA') }}</label>
                                
                                <div class="col-md-6">
                                    <input type="file" class="form-control @error('image') is-invalid @enderror" name="image"  >
    
                                    @error('image')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
    
                                    <br>
                                </div>
                            </div>
</form>
k97glaaz

k97glaaz1#

您的enctype=”multipart/form-data”没有正确设置,它使用了奇怪的"(dobule quotes),看到它们像而不是",所以可能您没有设置enctype,因此没有文件被发送...

gcxthw6b

gcxthw6b2#

我的问题就是这样解决的!
1.确保您已添加
enctype="multipart/form-data"
1.输入类型应为文件
<input type="file" name="image">
然后在验证中
'image' => 'file|mimes:jpg,jpeg,png,gif|max:1024',

相关问题