php - 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?).');
}
- 我公开制作了文件夹图像
- 我尝试在我的 php 系统文件中启用 phpfile 信息
没有任何效果。有什么帮助吗?作为一名开发人员,我已经 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>
解决方案
推荐阅读
- flutter - 如何将 appState(提供者)传递到 initState 中的屏幕?
- sql - 计算百分比 SQL
- css - 计算 font-size 与其容器宽度的比例
- google-maps - Flutter google_maps 捏缩放
- c# - 我正在尝试使用 Selenium C# 验证 Rows(2 - 25) 是否小于或等于 Row1。我不知道如何从那里开始
- android - NotFoundException: 绑定字符串资源时字符串资源 ID #0x0
- python - 在没有 Python 类的新实例的情况下调用更改方法
- python - 熊猫数据框填充了“NaN”,而不是正确的值
- javascript - 删除子元素会导致内存泄漏吗?
- google-cloud-platform - 无法从数据融合实例中删除自定义插件