Laravel 5.0,无法重新声明class App \ models \ Category

问题描述:

我最近将项目从laravel 4.2升级到了laravel 5.0,并且一直面临着一些错误。Laravel 5.0,无法重新声明class App models Category

我没有在4.2版本定义任何命名空间,但建议here,

我已经开始在我的代码定义命名空间。我不知道我面临的问题是否与此有关,但是发生在这个过程中。

我收到以下错误,同时运行的代码:

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 

这里是我的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'); 
    } 
} 

我搜索上这样类似的错误,但没有找到任何。

这是我的控制器在“/”路由上调用的函数。

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; 
    } 

让我知道你是否需要代码中的其他东西。我一直试图找出解决方案相当长一段时间了。

+0

你有另一个'Category Class'在相同的命名空间吗? – DavidDomain

+0

不,我不知道。 我之前在全局名称空间中已经拥有了所有内容,因此当时我只能想到这个错误。其次,这个类别位于App \ models命名空间中,并且在此命名空间中没有任何其他名为model的类。 – Yashasvi

+0

错误指向Category.php:19,这就是文件结束的地方。那是什么意思? – Yashasvi

这是因为您已经生成了一个控制器,然后将其拖到子文件夹中。 您需要将名称空间更改为正确的名称空间或正确生成控制器。

php artisan make:controller Api/CategoryController 

或命名空间更改为

namespace App\Http\Controllers\Api; 

(如果API是控制器在文件夹的名称)

我知道这个问题是旧的,但我会回答什么都有为我工作。 我最近在git分支上测试Laravel 4.2到5.0升级。我在名为Megaloquent的Model类中遇到了同样的问题,它在Laravel 4.2中扩展了Eloquent,现在是Model。

因为我试图得到它的开头没有命名空间的工作,我添加了应用程序/ subdirecties在composer.json

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

的类映射和很多的麻烦得到它的工作后,我决定在控制器和模型中使用命名空间,现在我发现它更加结构化和精简。你应该从classmap中删除app/Controllers-Models-Libraries,因为psr-4已经加载了app/{子目录}/classes中的所有类,并且classmap autoload使它发生了两次。你会删除它们

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

这是Laravel内部配置,你不能真正控制,但除去它们有固定的错误后,我仅此得到。