0

I am using Laravel php framework and I build my table like this

    Schema::create('template_blocks', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('content_template_id')->unsigned();
        $table->string('type')->nullable();
        $table->integer('order')->default(0)->unsigned();
        $table->integer('parent')->nullable();
        $table->text('meta')->nullable();
        $table->timestamps();

        $table->foreign('content_template_id')->references('id')->on('templates')->onDelete('cascade')->onUpdate('cascade');
    });

As you see I have:

$table->integer('parent')->nullable();

which points to another row in the same table that is the parent. When I delete the parent I would like mysql to also delete children, the same way that it cascades delete when you delete a row in foriegn table that has relationship with current table.

Is this possible with mysql or would I have to delete these manually on the server when I delete the parent? Having mysql do it would be best but if it can't be done then please let me know.

niko craft
  • 2,893
  • 5
  • 38
  • 67
  • 1
    https://stackoverflow.com/questions/14174070/automatically-deleting-related-rows-in-laravel-eloquent-orm follow this url i think this will help you. – Kuldeep Mishra Sep 09 '17 at 11:51
  • 1
    so, why not set a foreign key to the same table ([example](https://laracasts.com/discuss/channels/eloquent/model-with-foreign-key-in-same-table)), yet different column? Make sure to make the parent field `unsignedInteger`. – Jeffrey Sep 09 '17 at 12:32

0 Answers0