Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->string('image')->nullable();
$table->integer('user_id')->unsigned();
$table->integer('category_id');
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
---->
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table posts
add constraint posts_category_id_foreign
foreign key (category_id
) references categories
(id
) on delete cascade)