Currently, you can use the table as the first parameter of the ->constrained() method:
$table->foreignIdFor(User::class, 'author_id')->constrained('users')->cascadeOnDelete();
You can try sending a PR to fix that behavior
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have an Authors eloquent model, which points to the users table via protected $table = 'users', but when adding ->constrained()->cascadeOnDelete(), laravel appears to ignore the $table attribute on the model and instead looks for an authors table which doesn't exist!
My author_brands pivot table migration:
public function up(): void {
if(!Schema::hasTable('author_brands')) {
Schema::create('author_brands', static function (Blueprint $table) {
$table->foreignIdFor(User::class, 'author_id')->constrained()->cascadeOnDelete();
$table->foreignIdFor(Brand::class, 'brand_id')->constrained()->cascadeOnDelete();
Database::addResourceTimestamps($table);
$table->primary(['author_id','brand_id']);
});
}
}
... produces the following error ...
SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'authors' (Connection: mysql, SQL: alter table `author_brands` add constraint `author_brands_author_id_foreign` foreign key (`author_id`) references `authors` (`id`) on delete cascade)
How do I get laravel to use the $table property as intended?
Please or to participate in this conversation.