I’ve created my first api in Laravel, and I’m having trouble generating the database from the migations. Every time I run php artisan migrate
even after using composer dump-autoload
and generating again the database it doesn’t work. It gives me the error Cannot declare class CreateUsersTable, because the name is already in use
.
I’ve tried to clear the cache with composer clearcache
and php artisan cache:clear
but the same error keeps appearing.
Why is that if the there isn’t any migration table in the database ?
I’ve renamed the migrations and seeders files, so I had an easier time seeing what was created before and after and know it has the next structure. I won’t be adding more table in the future. And below there are the code for the first table in case is for some help referencing the naming conventions or rules.
This is my user migration:
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
My user model:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class User extends Model
{
use HasFactory;
}
My user seeder:
<?php
namespace DatabaseSeeders;
use IlluminateDatabaseSeeder;
class PlatformSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
AppModelsUser::factory()->count(5)->create();
}
}
2
Answers
At the end I ended up changing back the names files adding the date format that laravel uses and works again. So the lesson might be not messing with the format.
Check your migrations table. It may already have a migration record with the same name. Try running
php artisan migrate:fresh
instead.