1

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!

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Raymond Eiber
  • 23
  • 1
  • 5
  • In your DB enable default value of model_type as _null_ – Taacoo Apr 05 '18 at 10:59
  • 1
    Where is the `model_type` column in the migration? – user3574492 Apr 05 '18 at 11:02
  • MySQL error says, that you need to set up a default value for `model_type` field. You can set up `nullable()` inside migration, or `default('your_default_value')` – Tarasovych Apr 05 '18 at 11:21
  • If i change the model_type then it will just skip to the next column and say the same thing. All settings should run out of the box from the package. Thats why i assume their is a problem somewhere else – Raymond Eiber Apr 05 '18 at 11:39
  • Well 2 things. Like @user3574492 said. The column isnt even in your migration but okay. Secondly. if you get the error on a second column, add Nullable to the column aswell. Personally I always add ``->nullable()`` to most of my migration columns. Just to prevent this issue – Taacoo Apr 05 '18 at 12:40

1 Answers1

0

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