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?
>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.