首页 > 解决方案 > symfony mime 错误尝试在 laravel 7 中以 crud 形式上传图像

问题描述

我正在做一个 laravel7 项目

我想做一个简单的crud,没有身份验证,具有一对多的关系,并且表单的元素之一是图像。

问题是我尝试使用各种解决方案,我在网上搜索 laravel image crud 示例,但是当我尝试使用图像作为输入创建产品时,我一直遇到这个错误:

Symfony Exception

LogicException

Unable to guess the MIME type as no guessers are available (have you enabled the php_fileinfo extension?).

  if (!$this->isGuesserSupported()) {            
     throw new LogicException('Unable to guess the MIME type as no guessers are available (have you 
    enabled the php_fileinfo extension?).');        
}

没有任何效果。有什么帮助吗?作为一名开发人员,我已经 6 个月大了,所以如果提出愚蠢的问题,我深表歉意

类:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Color extends Model
{
    protected $fillable = [

        'namecol',
        'imgcol',

    ];

    public function products() {

        return $this -> hasMany(Product::class);
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [

        'namepro',
        'imgpro',

    ];

    public function color() {

        return $this -> belongsTo(Color::class);
    }
}

迁移:

    public function up()
    {
        Schema::create('colors', function (Blueprint $table) {

            $table -> id();

            $table -> string('namecol', 100) -> unique();
            $table -> string('imgcol') -> nullable();

            $table -> timestamps();
        });
    }


    public function down()
    {
        Schema::dropIfExists('colors');
    }

        Schema::create('products', function (Blueprint $table) {

            $table -> id();

            $table -> string('namepro', 100);
            $table -> string('imgpro') -> nullable();

            $table -> bigInteger('color_id') -> unsigned();
            
            $table -> timestamps();
        }); 
    }

 
    public function down()
    {
        Schema::dropIfExists('products');
    }

    public function up()
    {
        Schema::table('products', function (Blueprint $table) {

            $table -> foreign('color_id', 'product-color')
                    -> references('id')
                    -> on('colors');

        });
    }


    public function down()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->dropForeign('product-color');
        });
    }

创建和存储产品

    public function create() {

        $colors = Color::all();
        return view('pages.product-create', compact('colors'));
    }

    public function store(Request $request) {

        $data = $request -> all();


        $request->validate([
            'namepro' => 'required|min:3|max:100',
            'imgpro' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',

        ]);

        $path = $request->file('imgpro')->store('public/images');
        $product = new Product;
        $product->namepro = $request->namepro;
        $product->imgpro = $path;



        $color = Color::findOrFail($data['color_id']);
        $product = Product::make($request -> all());
        $product -> color() -> associate($color);
        $product -> save();

        
        return redirect() -> route('product-show', $product -> id);
    }

视图中的表格


    <form action="{{ route('product-store') }}" method="POST" enctype="multipart/form-data"> 
        @csrf
        @method('POST')

        <label for="namepro">namepro</label>
        <input name="namepro" type="text">
        <br>

        <label for="imgpro">imgpro</label>
        <input name="imgpro" type="file">
        <br>


        <label for="color_id">color</label>
        <select name="color_id">
            @foreach ($colors as $color)
                <option value="{{ $color -> id }}">

                    {{$color -> namecol}}

                </option>
            @endforeach
        </select>
        <br>



        <input type="submit">

    </form>

标签: phplaraveluploadcrud

解决方案


推荐阅读