Zoul's avatar
Level 5

Cannot declare class because the name is already in use

Hi all,

I don't have duplicate table with the same, i tried to remove this table to see it passes but i got another error with other table too. also cleared cach but nothing changed. Any idea pls ?

 Cannot declare class App\Models\CreateGalleriesPhotosTable, because the name is already in use

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


   Whoops\Exception\ErrorException 

  Cannot declare class App\Models\CreateGalleriesPhotosTable, because the name is already in use

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

  1   vendor\filp\whoops\src\Whoops\Run.php:510
      Whoops\Run::handleError("Cannot declare class App\Models\CreateGalleriesPhotosTable, because the name is already in use", "D:\xampp\htdocs\db\code\oaahdv1\database\migrations21_06_11_090235_create_galleries__photos_table.php")

  2   [internal]:0
      Whoops\Run::handleShutdown()

in CreateGalleriesPhotosTable table

class CreateGalleriesPhotosTable extends Migration  
{
    /**
     * Run the migrations.
     *
     * @return void 
     */
    public function up()
    {
        Schema::create('galleries_photos', function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(Gallery::class)->constrained()->onDelete('cascade');
            $table->string('photo')->nullable();
            $table->boolean('status')->default(false);
            $table->timestamps();
        });
    }
in galleries table

Copy

class CreateGalleriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('galleries', function (Blueprint $table) {
            $table->id();
            $table->text('title')->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
        });
    }
0 likes
10 replies
vincent15000's avatar

The class is already in use and not the table.

The problem seems to be because you have a model class called CreateGalleriesPhotosTable and a migration class called CreateGalleriesPhotosTable.

Zoul's avatar
Level 5

I appreciate your help @vincent15000 ! This is weird, i search the whole project and i did not find any, i tried to delete any class or migration table , then i got aonther issue which other class the same.

1 like
Zoul's avatar
Level 5

@vincent15000 Sure in galleries_photos migration table

<?php
namespace App\Models;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateGalleriesPhotosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void 
     */
    public function up()
    {
        Schema::create('galleries_photos', function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(Gallery::class)->constrained()->onDelete('cascade');
            $table->string('photo')->nullable();
            $table->boolean('status')->default(false);
            $table->timestamps();
        });
    }

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

in galleries migration table

<?php
namespace App\Models;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateGalleriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('galleries', function (Blueprint $table) {
            $table->id();
            $table->text('title')->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
        });
    }

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

in Gallery model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use Carbon;
use Image;
use File;
use Illuminate\Support\Str;
class Gallery extends Model
{
    use HasFactory;
    use HasTranslations;  
    protected $fillable = ['title','description'];
    protected $table = 'galleries';    

    public $translatable = ['title','description'];


    public function galleryPhoto(){
        return $this->hasMany(GalleryPhoto::class,'gallery_id');
    }
    public static function boot() { 
        parent::boot();
        static::deleting(function($gallery) {
            $gallery->galleryPhoto->each(function($gphoto) {
                $image_delete = Str::after($gphoto->photo, 'images/');
                File::delete('storage/images/'.$image_delete);
                $gphoto->delete();
            });
         
        });
    } 
}

in GalleryPhoto model

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class GalleryPhoto extends Model
{
    use HasFactory;   
    protected $fillable = ['photo'];
    protected $table = 'galleries_photos';    

    public function getPhotoAttribute($val)
    {
        return $val ? asset('storage/images/galleries/'.$val) : '';      
    }        
    public function gallery()
    {
        return $this->belongsTo(Gallery::class,'gallery_id');
    }
}
1 like
Zoul's avatar
Level 5

Thanks a lot for your help @jlrdw, i had to delete all migratioin tables and models that are causing the issue and re-created them using php artisan make:model ModelName -m which created the migration table along with Model, and now migration tables are anonymuous like return new class extends Migration I dont have issue now but still having some other migration table named like CreateGalleriesPhotosTable extends Migration Do you recommand to re-create them all since my app is not in production yet ?

puklipo's avatar

Write your Laravel version.

Duplicate class errors were common in older versions. Current versions use anonymous classes so there are no duplicate errors.

Migration files are not namespaced. You're using migrations incorrectly.

3 likes
Zoul's avatar
Level 5

Many thanks @puklipo for your help!

Now i was using version 8 before upgrading to larvel 11.x thats why i have these migration tables created in old style ,i'm wondering if i should recreate all migration tables now

Snapey's avatar

Your migration file should not be in the models folder!

You sure you don't also have file of the same name in the database/migrations folder?

2 likes
Zoul's avatar
Level 5

I appreciate your help @Snapey ! Yes the error indicating i'm having migration file under models folder, i searched everywhere inside the project and could find it, finally i had to recreate all migration file along with their model class and the issue resolved, but this is no the best practice, i'm wondering if there is a better way to tackle this issue with the app being in production

Please or to participate in this conversation.