jeudi 28 juin 2018

Laravel 5.6 OnDelete cascade isnt working

I have been looking through other posts on here trying to figure this issue out, specifically the ones I linked at the bottom of the page. None of which have seemed to help me solve this issue. If this isnt allowed please remove and my apology's.

I have 3 different tables all of which have one to many relationships

servers->Incidents->incident_notes

I set the onDelete cascade for the foreign keys yet when I go to delete a server the incidents related to that i.d. stay. Below is the Migrations for the Tables as well as the destroy() methods. I am using laravel 5.6, on a wamp server, and using mysql and phpmyadmin!

servers table:

Schema::create('servers', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('url');
    $table->boolean('isPublic');
    $table->timestamps();
});

incidents Table:

Schema::create('incidents', function (Blueprint $table) {
    $table->engine = 'InnoDB'; 
    $table->increments('id');
    $table->string('title');
    $table->mediumText('body');
    $table->mediumText('status');
    $table->integer('server_id')->unsigned();
    $table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade');
    $table->timestamps();
});

incident notes table:

Schema::create('incident_notes', function (Blueprint $table) {
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->mediumtext('body');
    $table->integer('incident_id')->unsigned();
    $table->foreign('incident_id')->references('id')->on('incidents')->onDelete('cascade');
    $table->timestamps();
});

Only posting one destroy because they are all pretty much the same code:

public function destroy($id)
    {
        $server = Server::find($id);
        $server->delete();

        return redirect('/dashboard')->with('success', 'Server deleted');
    }

Delete all posts related to a user in laravel

onDelete('cascade') has no effect Laravel 5.6

Laravel foreign key onDelete('cascade') not working



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire