Неизвестный столбец 'imageable_type' в 'списке полей' при вставке полиморфного отношения «один к одному» laravel 5 - PullRequest
0 голосов
/ 28 февраля 2019

Мой Изображение Миграция

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 в контроллере 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))

То есть нет столбца 'imageable_type' в моей таблице 'images', когда он на самом деле находится в таблице

Будут оценены любые указатели.

// Решено

Я изменил миграцию изображения на:

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

, что ставитстолбец 'imageable_type' перед столбцом 'imageable_id' и теперь он работает.

1 Ответ

0 голосов
/ 28 февраля 2019

Это потому, что у вас есть 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();
});
...