I want to delete a record in category
table wherein it will also delete the subcategory
record which has a the foreign key
of category
. How can I do this?
Also, an explanation would help as why it has happened. Thank you!
Controller
public function destroy(Category $category)
{
// return $category;
Category::where('id', $category->id)->delete();
Subcategory::where('category_id', $category->id)->delete();
return back();
}
Category migration
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Sub-category mgiration
Schema::create('subcategories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('category_id')->unsigned();
$table->string('subcatname');
$table->string('name');
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories');
});