kmugi
kmugi

Reputation: 363

Laravel namespace error: Cannot use A as A because the name is already in use

I'm facing an issue where a 'use' statement in my controller throws

Cannot use App\Enums\Division as Division because the name is already in use

The Division enum is structured as follows:

namespace App\Enums;

use App\Enums;

class Division extends Enum
{
...
}

This enum is being used in a Model as follows:

namespace App;

use Carbon\Carbon;
use App\Enums\Division;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Employee extends Model
{
...
}

The controller where this exception is thrown from has the following structure:

namespace App\Http\Controllers;

use Carbon\Carbon;
use App\Employee;
use App\Division;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AttendanceController extends Controller
{
...
}

EmployeeController has the same structure but didn't throw any namespace collision errors

namespace App\Http\Controllers;

use Carbon\Carbon;
use App\Employee; 
use Illuminate\Http\Request;
use App\Enums\Month;
use App\Division;
use Illuminate\Support\Facades\Auth;

class EmployeeController extends Controller
{
...
}

I'm not sure why this is causing an issue as I use the same structure for my EmployeeController class and it's working flawlessly. NOTE: I also have a model called Division, hence my AttendanceController has "use App\Division", not to be confused with just the enum file.

Some suggested that I need an alias for App\Enums\Division, but my EmployeeController didn't complain and I didn't need aliasing. Others suggest that it's a php issue - I'm on 7.0.12. Any help with this would be greatly appreciated!

Upvotes: 0

Views: 3014

Answers (4)

Asadut Zaman
Asadut Zaman

Reputation: 313

I have faced the same issue. I have solved the issue. I have used the same model name twice. So I have removed one model name and the issue is solved.

Upvotes: 0

Daniel Protopopov
Daniel Protopopov

Reputation: 7256

If Enum class is within App\Enums namespace, and so is the Division, you do not need to specify to use App\Enums in the Division PHP file. In the controller class, however, you need to specify correct paths to the classes of Enum and Division as

// Division file
namespace App\Enums;

class Division extends Enum
{ ... }

// Controller File
namespace App\Http\Controllers;

use App\Enums\Division; 
use App\Enums\Enum;

class AttendanceController extends Controller
{ ... }

Upvotes: 1

IMSoP
IMSoP

Reputation: 98015

It's hard to be sure without the full code (I'll update or delete this if better examples are added to the question), but it sounds very likely that your file contains both:

use App\Division;

and:

use App\Enum\Division;

These are both using the implicit alias as Division, so PHP is complaining that when it sees the word Division, it doesn't know which alias is meant.

The fix is to add an as clause to one or both lines to give them some other alias which is not ambiguous.

Upvotes: 0

Tom
Tom

Reputation: 3351

To avoid having the same class 'used' multiple times, alias them. Assuming App\Division is a model:

use App\Division as DivisionModel;

And update the references to Division accordingly.

Upvotes: 2

Related Questions