thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

Menu Builder > Items : Deleting a Parent let rubbish children in the database #5418

Open Ronan-Lenor opened 3 years ago

Ronan-Lenor commented 3 years ago

Laravel version

8.53

PHP version

7.4.14

Voyager version

1.x-dev 1102296

Database

MySQL 5.7.32

Description

Deleting a Parent MenuItem let its children with a a dead key in parent_id and they don't appear on the User Interface.

Steps to reproduce

In the menu builder, if i create a parent (which in the database has a something like id = 1 & parent_id = null) and that i associate a children to this parent (then the children has id = 2 & parent_id = 1) and that if i delete the parent, then the child remain in the databse with a dead key with parent_id = 1 (which doesn't exist anymore).

Expected behavior

a Cascading deletation of all menu items children would be great

Screenshots

menuitem

Additional context

No response

Ronan-Lenor commented 3 years ago

Here is a migration proposal to resolve it for the bugged version of voyager.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlterMenuItem extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('menu_items', function (Blueprint $table)
        {
            $table->unsignedInteger('parent_id')->nullable()->change();

            $table->foreign('parent_id')->references('id')->on('menu_items')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('menu_items', function (Blueprint $table)
        {
            $table->dropForeign(['parent_id']);
        });

        Schema::table('menu_items', function (Blueprint $table)
        {
            $table->integer('parent_id')->nullable()->change();
        });
    }
}