Laravel 5 插入一对一多态关系laravel 5时,“字段列表”中的未知列“imageable_type”

Laravel 5 插入一对一多态关系laravel 5时,“字段列表”中的未知列“imageable_type”,laravel-5,polymorphic-associations,Laravel 5,Polymorphic Associations,我的图像迁移 class CreateImagesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('images', function (Blueprint $table) { $table->increments('id'); $table->string('url

我的图像迁移

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');
}
}
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();
});
我的图像模型

我的商店模型

所以我有商店、商品、图像模型,一个商店/一个商品只能拥有一个图像

我正在尝试保存一个存储,并且在StoreController的“存储”操作中,一个图像属于该存储:

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

}
我遵循这个例子,但它不起作用

以下是错误:

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”表中没有“imageable_type”列,而实际上它在表中

任何指点都将不胜感激

//解决

我将图像迁移更改为:

public function up()
{
    Schema::create('images', function (Blueprint $table) {
        $table->increments('id');
        $table->string('url');
        $table->morphs('imageable');
        $table->timestamps();
    });
}
这将“imageable\u type”列置于“imageable\u id”列之前,现在它可以工作了。

这是因为迁移中有一个空白

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

哦,你说得对!!!!非常感谢。我应该使用$table->morphs'imageable'来避免这样的打字错误。