Forrest
Forrest

Reputation: 721

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

My Image migration

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

My Image model

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'
];
}

My Store model

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',
];
}

So I have Store,Item,Image models and a store/an item can own only one image.

And I am trying to save a store and an image belongs to that store in the 'store' action of the 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

}

I am following the example here but it does not work

Here is the 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))

Which is saying there is no column 'imageable_type' in my 'images' table when it's actually in the table

Any pointers will be appreciated.

// Solved

I changed the Image migration into:

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

which puts the column 'imageable_type' before the column 'imageable_id' and now it works.

Upvotes: 0

Views: 654

Answers (1)

Kapitan Teemo
Kapitan Teemo

Reputation: 2164

It's because you have a white space in your migration

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

Upvotes: 1

Related Questions