我在使用 Spatie 媒体库包设置 Laravel 时遇到问题。
这是我得到的错误:
SQLSTATE[HY000]: General error: 1364 Field 'model_type' doesn't have a default value (SQL: insert into `images` (`updated_at`, `created_at`) values (2018-04-05 10:38:39, 2018-04-05 10:38:39))
我通过 Homebrew 运行 MySQL 并停用了严格模式。我还在 config/database.php 文件中将严格模式设置为 false。
控制器 | 图像控制器.php
<?php
namespace App\Http\Controllers;
use App\Image;
use Illuminate\Http\Request;
class ImageController extends Controller
{
public function addBackgroundImage(Request $request)
{
Image::create()
->addMediaFromRequest('background')
->toMediaCollection('backgrounds')
->save();
return redirect('/settings')->with('success', 'Hintergrund Bild hinzugefügt');
}
}
型号 | 图片.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\HasMedia;
class Image extends Model implements HasMedia
{
use HasMediaTrait;
}
查看 | settings.blade.php
{!! Form::open(['action' => 'ImageController@addBackgroundImage', 'method' => 'POST', 'files' => true]) !!}
<div class="file-field input-field">
<div class="btn">
<span>Background</span>
{{Form::input('file', 'background')}}
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<button class="btn waves-effect waves-light" type="submit" >Speichern
<i class="material-icons right">send</i>
</button>
{!! Form::close() !!}
路线 | 网页.php
Route::post('/settings', 'ImageController@addBackgroundImage')->name('background.store');
移民
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->morphs('model');
$table->string('collection_name');
$table->string('name');
$table->string('file_name');
$table->string('mime_type')->nullable();
$table->string('disk');
$table->unsignedInteger('size');
$table->json('manipulations');
$table->json('custom_properties');
$table->json('responsive_images');
$table->unsignedInteger('order_column')->nullable();
$table->nullableTimestamps();
});
设置
- laravel/框架:“5.6.*”
- spatie/laravel-medialibrary: "^7.0.0"
谢谢你的帮助!