modelruntime-errorlaravel-5redeclaration

Laravel 5.0, Cannot redeclare class App\models\Category


I recently upgraded my project from laravel 4.2 to laravel 5.0 and have been facing several errors.

I didn't define any namespaces in 4.2 version, but as suggested here,

I have started defining namespaces in my code. I don't know if the problem that I am facing is related to that, but it occured in the middle of this process.

I am getting the following error while running the code:

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with
message 'Cannot redeclare class App\models\Category' in  
/Users/yash/summers/lightsCameraDinner/lcd_updated/app/models/Category.php:19

Here is my Category.php:

<?php namespace App\models;

use Eloquent;

class Category extends Eloquent {

  protected $table = 'categories';
  protected $guarded = array('id');

  // Defining 'Many to Many' Relationship with 'VendorProfile' Model
  public function clients() {
    return $this->belongsToMany('Client');
  }

  // Defining 'One to Many' Relationship with 'Job' Model
  public function jobs() {
    return $this->hasMany('Job');
  }
}

I searched for the similar errors on SO, but didn't find any.

This is the function in my controller that is called on "/" route.

    public function getIndex() {
    $categories = Category::all();

    $messages = Message::groupBy('receiver_id')
                ->select(['receiver_id', \DB::raw("COUNT('receiver_id') AS total")])
                ->orderBy('total', 'DESC')
                ->get()
                ->toArray();

    $vendors_ids = array();
    foreach ($messages as $message) {
      $vendors_ids[] = $message['receiver_id'];
    }

    $clients = Client::where('profile_type', 'VendorProfile')
                      ->where('is_activated', 1)
                      ->whereIn('id', $vendors_ids)
                      ->limit(4)
                      ->get();

    if($clients->count() < 4) {
      $clients = Client::where('profile_type', 'VendorProfile')
                        ->where('is_activated', 1)
                        ->limit(4)
                        ->get();
    }   
    Log::info('getIndex function of PagesController');
    $this->layout = View::make('layouts.homepage');
    $this->layout->content = View::make('pages/index', ['categories' => $categories, 'clients' => $clients]);
    return $this->layout;
  }

Let me know if you need anything else from the code. I have been trying to figure out the solution for quite some time now.


Solution

  • It is because you have generated a controller and then dragged it to a subfolder. You need to ether change the namespace to the right one or generate your controller correctly.

    php artisan make:controller Api/CategoryController  
    

    or change the namespace to

    namespace App\Http\Controllers\Api;
    

    (if api is the name of the folder the controller is in)