Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

laravel cascadeOnDelete not found

I’m building an API with laravel, and I’m trying to get the migrations to work. However, this migration fails with Method Illuminate\Database\Schema\Blueprint::cascadeOnDelete does not exist.

public function up(): void
{
    Schema::create('stuff', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('image');
        $table->timestamps();

        $table->cascadeOnDelete();
    });
}

My app is laravel 10, but this feature is in the docs for laravel 10 and 11. How is it that this method doesn’t exist?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The cascadeOnDelete() method is available on the class Illuminate\Database\Schema\ForeignIdColumnDefinition and not on the class Illuminate\Database\Schema\Blueprint, and this is logical because you add the CASCADE constraints on foreign keys.

You probably meant to write something like this:

$table->foreignId("key_id")->constrained()->cascadeOnDelete();

Notice that all foreign key constraints are added after the constrained() method.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading