插入一对一多态关系 laravel 5 时 'field list' 中的未知列 'imageable_type'

Unknown column 'imageable_type' in 'field list' when insert one-to-one polymorphic relation laravel 5

我的图片迁移

class CreateImagesTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('images', function (Blueprint $table) {
        $table->increments('id');
        $table->string('url');
        $table->integer('imageable_id');
        $table->string(' imageable_type');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('images');
}
}

我的图片模型

class Image extends Model
{
/**
 * Get the store of the image
 */
public function store()
{
    return $this->morphTo('App\Store', 'imageable');
}

/**
 * Get the item of the image
 */
public function item()
{
    return $this->morphTo('App\Item', 'imageable');
}

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'url', 'imageable_id', 'imageable_type'
];
}

我的店铺型号

class Store extends Model
{
/**
 * Get the user that owns the store.
 */
public function user()
{
    return $this->belongsTo('App\User');
}

/**
 * Get the items of a store
 */
public function items()
{
    return $this->hasMany('App\Item');
}

/**
 * Get the store's image.
 */
public function image()
{
    return $this->morphOne('App\Image', 'imageable');
}

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'address', 'description','user_id',
];
}

所以我有 Store、Item、Image 模型,并且 store/an 项目只能拥有一张图像。

并且我正在尝试在 StoreController 的 'store' 操作中保存商店和属于该商店的图像:

public function store(Request $request){
    $request->validate(....);

    $store = $user->stores()->create($request->all());

    // Upload the image to s3 and retrieve the url
    ...
    $url = Storage::disk('s3')->put($path, $image);
    Storage::cloud()->url($path);

    // Trying to save the image to databse
    $image = new Image(['url' => $url]);
    $store->image()->save($image); // => Error

}

我正在按照示例进行操作 here 但它不起作用

这是错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'imageable_type' in 'field list' (SQL: insert into `images` (`url`, `imageable_type`, `imageable_id`, `updated_at`, `created_at`) values (images/stores/1/1551316187.jpg/Rw7BQvSeIHvNX3ldFc0GUufmcFEIAi6TiITteDyr.jpeg, App\Store, 1, 2019-02-28 01:09:49, 2019-02-28 01:09:49))

也就是说在我的 'images' table 中没有列 'imageable_type' 而它实际上在 table

任何指点将不胜感激。

// 已解决

我把图片迁移改成了:

public function up()
{
    Schema::create('images', function (Blueprint $table) {
        $table->increments('id');
        $table->string('url');
        $table->morphs('imageable');
        $table->timestamps();
    });
}

将列 'imageable_type' 放在列 'imageable_id' 之前,现在可以使用了。

这是因为您的迁移中有一个white space

Schema::create('images', function (Blueprint $table) {
    $table->increments('id');
    $table->string('url');
    $table->integer('imageable_id');
    $table->string(' imageable_type'); <---should be $table->string('imageable_type')
    $table->timestamps();
});