Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PT-83's avatar
Level 7

Cannot declare class because the name is already in use?

Hi all,

I am using Laravel 8 with Spark. I am in the process of setting up Stripe (through documentation) and reach an error that I am failing to understand.

In the terminal, trying to php artisan migrate I receive the following error: Cannot declare class CreateReceiptsTable, because the name is already in use. If I try to visit the localhost I receive the following errorSQLSTATE[42S02]: Base table or view not found

Things I have tried - php artisan migrate:fresh php artisan migrate:rollback php artisan config:cache and composer dump_autoload however did not address the issue.

Laravel picked up the error, and asked if I would like to run the migration, which I did. Suddenly, all tables became present. Once I login (creating a new user) Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'trial_ends_at' in 'field list' error occurs.

CreateReceiptsTable Migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateReceiptsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('receipts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('billable_id');
            $table->string('billable_type');
            $table->unsignedBigInteger('paddle_subscription_id')->nullable()->index();
            $table->string('checkout_id');
            $table->string('order_id')->unique();
            $table->string('amount');
            $table->string('tax');
            $table->string('currency', 3);
            $table->integer('quantity');
            $table->string('receipt_url')->unique();
            $table->timestamp('paid_at');
            $table->timestamps();

            $table->index(['billable_id', 'billable_type']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('receipts');
    }
}

Log details

"} 
[2021-05-29 12:10:04] local.ERROR: Cannot declare class CreateReceiptsTable, because the name is already in use {"exception":"[object] (Symfony\Component\ErrorHandler\Error\FatalError(code: 0): Cannot declare class CreateReceiptsTable, because the name is already in use at /Users/dad/Desktop/projects/Sumpaper/vendor/laravel/spark-stripe/database/migrations/2019_06_03_000003_create_receipts_table.php:7)
[stacktrace]
#0 {main}
"} 
0 likes
13 replies
amirkamizi's avatar

have you tried changing the class name to something else? I mean if instead of

class CreateReceiptsTable extends Migration

you write

class CreateReceiptsSecondTable extends Migration

what happens?

PT-83's avatar
Level 7

Changed class to CreatesReceiptsSecondTable ran php artisan migrate output:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'receipts' already exists (SQL: create table `receipts` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `provider_id` varchar(255) not null, `amount` varchar(255) not null, `tax` varchar(255) not null, `paid_at` timestamp not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

and when visiting localhost/billing

lluminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'subscriptions.user_id' in 'where clause' (SQL: select * from `subscriptions` where `subscriptions`.`user_id` = 3 and `subscriptions`.`user_id` is not null order by `created_at` desc)
amirkamizi's avatar

the error changed. that's a good sign. now it says the table already exists. I think you should migrate:fresh. so it would delete the tables. it wants to create the table but the table is already in the database.

PT-83's avatar
Level 7

The same error occurs even with the new CreatesReceiptsSecondTable class name.

Cannot declare class CreateReceiptsSecondTable, because the name is already in use

  at database/migrations/2019_05_03_000003_create_receipts_table.php:7
      3▕ use Illuminate\Database\Migrations\Migration;
      4▕ use Illuminate\Database\Schema\Blueprint;
      5▕ use Illuminate\Support\Facades\Schema;
      6▕
  ➜   7▕ class CreateReceiptsSecondTable extends Migration
      8▕ {
      9▕     /**
     10▕      * Run the migrations.
     11▕      *

      +1 vendor frames
  2   [internal]:0
      Whoops\Run::handleShutdown()
amirkamizi's avatar

okay now it's weird that it's happening again with the new class because apparently, what you do is trying to create the class again but when migrating it shouldn't create the class again it should only create the table.

I don't know what your code is but if i had this issue it's I would

1-

php artisan optimize:clear
composer dump-autoload
composer clear-cache

2- change the name from CreateReceiptsSecondTable to CreateReceiptsThirdTable to see what is my error ( probably the error would change)

3- migrate:fresh and if I get the

Cannot declare class CreateReceiptsThirdTable, because the name is already in use

then there is something else. it's not with the migration file only. it's probably your migration is not happening correctly.

in that case, I would know that "okay the issue is not just the class name as the error says".

if your problem is solved and by step 3 you don't get the error, perfect but even if you do, at least you're making some progress and debug the right place.

PT-83's avatar
Level 7

Yes, it's just repeating the same error even after all the above you mentioned.

 Cannot declare class CreateReceiptsThirdTable, because the name is already in use

Other than a few adding a few views, this install is fresh without code change. This has to do with Laravel Spark (Stripe) if I had to guess, since then I have been getting this error.

Also, when I run the migration and access my profile I should see billing options but it's not there.

As it's a fresh application, I don't know what's causing the exact issue because I haven't added code yet. I literally was just trying to get Spark to function first.

josecigarro's avatar

I had the same problem and solved it by adding the name of the migration file to the class name. example: migration: 2021_09_24_081300_create_profiles_categories_table class: CreateProfilesCategoriesTable

Before i had: class: CreateProfileCategoriesTable

Hope it help.

11 likes
dorqa95's avatar

@José Cigarro In my case the error was about I missed "Create" from the beginning of class name.

lditom's avatar

@José Cigarro Thank you so much for reminding me it's best to check the small things first <3 Correcting my class name to match the name of my migration resolved the issue!

omitobisam's avatar

As for me, the issue was that the classname was wrongly camel cased.

dylanglockler's avatar

This can happen if you're using the wrong namespace or have mismatched class name to filename. the command 'composer dump-autoload' at the terminal may illuminate the real problem.

2 likes

Please or to participate in this conversation.