Andrea
Andrea

Reputation: 73

Cannot declare class because the name is already in use in (same file)

I'm getting the following error while running my web app:

[Fri Jan 15 19:25:23 2021] PHP Fatal error:  Cannot declare class App\Http\Livewire\Customer because the name is already in use in /home/<username>/Projects/<project_name>/app/Http/Livewire/Customer.php on line 9

The declaration error is basically complaining about its own file and I'm not sure why. This is the content of the Customer.php file:

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\Customer;
use Illuminate\Support\Facades\Auth;

class Customer extends Component
{
    use WithPagination;
    
    public function render()
    {
        $customers = Customer::where('user_id', Auth::id())->paginate(9);
        return view('livewire.customer')->with(['customers' => $customers]);
    }
}

Can someone help me untackle this problem?

Upvotes: 2

Views: 9973

Answers (1)

mrbm
mrbm

Reputation: 2194

To resolve the conflict just use

use App\Models\Customer as AppCustomer;

Then your class can use AppCustomer instead of Customer.

$customers = AppCustomer::where('user_id', Auth::id())->paginate(9);

Upvotes: 6

Related Questions