phpmany-to-manylaravel-9laravel-migrationslaravel-controller

I cannot delete data which have many-to-many relationship in laravel 9


I am using PHP 8.2.0, MySQL 8.2.0, and Laravel 9.48.0. This is my database migrations file pivot table (category_menu):

public function up()
    {
        Schema::create('category_menu', function (Blueprint $table) {
            $table->foreignId('category_id')->constrained();
            $table->foreignId('menu_id')->constrained();
        });
    }

This is the CategoryController.php:

public function destroy(Category $category)
    {
        Storage::delete($category->image);
        $category->menus()->delete(); // many-to-many relationship
        $category->delete();
        return to_route('admin.categories.index');
    }

Lastly, this is the MenuController.php:

    public function destroy(Menu $menu)
    {
        Storage::delete($menu->image);
        $menu->categories()->delete(); // many-to-many relationship
        $menu->delete();
        return to_route('admin.menus.index');
    }

I have tried deleting one of the menu/category, but laravel keeps sending out an error:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (larareserve.category_menu, CONSTRAINT category_menu_menu_id_foreign FOREIGN KEY (menu_id) REFERENCES menus (id))

delete `menus` from `menus` inner join `category_menu` on `menus`.`id` = `category_menu`.`menu_id` where `category_menu`.`category_id` = 4

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (larareserve.category_menu, CONSTRAINT category_menu_category_id_foreign FOREIGN KEY (category_id) REFERENCES categories (id))

delete `categories` from `categories` inner join `category_menu` on `categories`.`id` = `category_menu`.`category_id` where `category_menu`.`menu_id` = 7

I thought, this error might have to do with the migration's pivot table (category_menu). But, I just don't know how to fix this. I am currently one month old learning the laravel framework and right now, I am working on the tutorial project from youtube. Please help (educate) me anyone. I am sorry for all inconveniences.


Solution

  • Alright. Already got the solution from Laravel Cannot delete or update a parent row: a foreign key constraint fails

    so what I did was, in my database migrations file pivot table (category_menu):

    public function up()
    {
        Schema::create('category_menu', function (Blueprint $table) {
            $table->foreignId('category_id')->constrained();
            $table->foreignId('menu_id')->constrained()->onDelete('cascade');
        });
    }
    

    CategoryController.php:

    public function destroy(Category $category)
    {
        Storage::delete($category->image);
        $category->menus()->delete(); // many-to-many relationship
        $category->delete();
        return to_route('admin.categories.index');
    }
    

    MenuController.php:

    public function destroy(Menu $menu)
    {
        Storage::delete($menu->image);
        //$menu->categories()->delete(); // many-to-many relationship
        $menu->delete();
        return to_route('admin.menus.index');
    }
    

    By adding ->onDelete('cascade') on the migration file, I can already delete the menu. I also put the syntax only on the menu item in the migration file and commented the line to delete the menu's category method on the MenuController, so that whenever I deleted the menu, the category won't be deleted. If I deleted the category, the menu related to it will also be deleted.