Laravel Spatie媒体库model_type没有默认值

i'm having trouble setting up Laravel with the Spatie Media Library package.

Here is the error i'm getting:

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))

I'm running MySQL via Homebrew and deactivated the strict mode. Also i set the strict mode to false in config/database.php file.

Controller | ImageController.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');
    }

}

Model | Image.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;

}

View | 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() !!}

Routes | web.php

Route::post('/settings', 'ImageController@addBackgroundImage')->name('background.store');

Migration

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();
});

Setup

  • laravel/framework: "5.6.*"
  • spatie/laravel-medialibrary: "^7.0.0"

Thanks for any help!

double2022
double2022 好吧2件事。喜欢@user3574492说。该列甚至在您的迁移中都没有,但没关系。其次。如果您在第二列上收到错误,请将Nullable添加到列中。我个人总是在大多数迁移列中添加->nullable()。只是为了防止这个问题
2 年多之前 回复
dsogx84602
dsogx84602 如果我更改了model_type,那么它将跳到下一列并说同样的事情。所有设置都应该从包中开箱即用。这就是为什么我认为他们在其他地方是一个问题
2 年多之前 回复
dongshanxun6479
dongshanxun6479 MySQL错误说,您需要为model_type字段设置默认值。您可以在迁移中设置nullable(),或者默认设置('your_default_value')
2 年多之前 回复
drsdvwsvo78320812
drsdvwsvo78320812 迁移中的model_type列在哪里?
2 年多之前 回复
douqiao6563
douqiao6563 在DB中,启用model_type的默认值为null
2 年多之前 回复

1个回答



一种方法是在Image模型上设置protected $ fillable属性(数组)。</ p>
</ div>

展开原文

原文

One way is to set the protected $fillable property (array) on the Image model.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问