6

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.

Community
  • 1
  • 1
Yash
  • 5,225
  • 4
  • 32
  • 65
  • Do you have another `Category Class` in the same namespace somewhere? – DavidDomain May 28 '15 at 10:47
  • No, I don't. I had everything in the global namespace before, so I would have got the error at that time only I think. Secondly, this category is in App\models namespace, and I don't have any other class named model in this namespace. – Yash May 28 '15 at 10:54
  • The error points to Category.php:19, that's where the file ends. What does that mean? – Yash May 28 '15 at 10:54
  • I've just created a Model with `artisan make:model`. My Model looks different than yours. `namespace App;`. And `use Illuminate\Database\Eloquent\Model;`. Might be it. Try it. – DavidDomain May 28 '15 at 11:26
  • Strange. I changed it to `namespace App` , but now PagesController says `App\Category`not found. – Yash May 28 '15 at 11:38
  • And `Eloquent` anyways refers to `Illuminate\Database\Eloquent\Model` in app.php – Yash May 28 '15 at 11:40
  • Add this to your controller `use App\Models\Category` – DavidDomain May 28 '15 at 11:40
  • `App\Models\Category` not found. By adding this, it looks into the namespace `App\Models` but my models are in namespace `App`. So, that does not make any sense I gues. – Yash May 28 '15 at 11:45

2 Answers2

11

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)

Adam
  • 3,415
  • 4
  • 30
  • 49
0

I know the question is old, but I will answer what has worked for me. I'm recently testing Laravel 4.2 to 5.0 upgrade on a git branch. I had the same problem with a Model class named Megaloquent that extends Eloquent in Laravel 4.2, which is now Model.

Because I was trying to get it to work without namespace at the beginning, I added the app/ subdirecties in the classmap of composer.json

"autoload": {
    "classmap": [
        "database",
        "app/Http/Controllers",
        "app/Http/Controllers/Auth",
        "app/Models",
        "app/Libraries"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

And after a lot of trouble getting it to work, I decided to go with namespace in Controllers and Models, which I find now more structured and precised. You should remove the app/ Controllers-Models-Libraries from classmap since psr-4 already loads all classes in app/{subdirectories}/ classes, and classmap autoload makes it happen twice. You'll get only this after removing them

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

That's Laravel internal configuration that you can't really control, but removing them has fixed the error for me.

KeitelDOG
  • 4,750
  • 4
  • 18
  • 33